9

I just want to do a rails action without a view.

In my 'routes.rb'

resources :pictures do
    member do 
        post 'dislike'  
    end  
end

In my 'PictureController.rb'
this does not work

def dislike
    @picture = Picture.find(params[:id])
    @like = Like.find(:user_id => current_user.id, :picture_id => params[:id])

    @like.destroy

    respond_to do |format|
        format.html { render :action => :show, :id => params[:id], notice: 'You don\'t  like this picture anymore.' }
        format.json { render json: @picture }
    end
end

neither do this

def dislike
    @picture = Picture.find(params[:id])
    @like = Like.find(:user_id => current_user.id, :picture_id => params[:id])

    @like.destroy

    respond_to do |format|
        format.html { redirect_to @picture, notice: 'You don\'t  like this picture anymore.' }
        format.json { render json: @picture }
    end
end

or even this (but this is not the case for me, i want a feedback to the user via json and via html)

def dislike
    @picture = Picture.find(params[:id])
    @like = Like.find(:user_id => current_user.id, :picture_id => params[:id])

    @like.destroy

    render :nothing => true
end

But i keep getting this error message:

ActionView::MissingTemplate: Missing template pictures/dislike, application/like with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :coffee]}.

How should i tell rails that this action in PicturesController does not needs a view?

Solved!

I didn't really solved the problem of telling rails i did not need a view, i just created another controller, put the method in it, and told rails routing to match the dislike action with a match call. I cannot tell for sure, but i think it was a problem with the resources :picture in my routes.rb file...

But anyway, thank you guys! =)

Crystian Leão
  • 695
  • 1
  • 7
  • 18

3 Answers3

26

Something like this?

def dislike
    @picture = Picture.find(params[:id]
    @like = Like.find(:user_id => current_user.id, :picture_id => params[:id])

    @like.destroy

    render :nothing => true
end
iouri
  • 2,807
  • 1
  • 12
  • 11
5

Just created another controller with the dislike action:

def dislike
    @picture = Picture.find(params[:id])
    @like = Like.find(:user_id => current_user.id, :picture_id => params[:id])

    @like.destroy

    respond_to do |format|
        format.html { redirect_to :back, notice: 'You don\'t  like this picture anymore.' }
        format.json { render json: @picture }
    end
end

and modified my routes.rb to match this action:

match 'pictures/:id/dislike' => "likes#dislike", :via => :post

and my link to dislike now is

<%= link_to 'Dislike!', {:action => :dislike, :controller => :likes}, :method => :post %>
Crystian Leão
  • 695
  • 1
  • 7
  • 18
0

Most likely, the problem is that you're hitting this action via an ajax request. So the controller is looking for format.js, but you haven't specified a response for that format in your block. Thus it's falling through to the default.

Try

format.js { render json: @picture }

You may also need to tell the ajax request to expect a json response.

gregates
  • 6,379
  • 27
  • 31
  • Actually it fails in both HTML via browser and the test cases – Crystian Leão Oct 16 '12 at 17:07
  • I assume you mean that you're entering the path in the browser bar to test the html render? In that case, you ought to get a routing error - you've defined a `post` route, not a `get`. – gregates Oct 16 '12 at 17:17
  • No, i've put a link to the action with ` :like}, :method => :post %>`, and the error i get is the error described in the question... – Crystian Leão Oct 16 '12 at 17:38
  • @CrystianLeão I think that generates `pictures/like` rather than `pictures/1/like`, correct? (1 can be any ID) – varatis Oct 16 '12 at 17:40
  • @varatis No, the link is correct, it points to `/pictures/980190963/like` (when the link is at pictures/980190963) – Crystian Leão Oct 16 '12 at 17:52