Rails controller action without a view

Crystian Leão picture Crystian Leão · Oct 16, 2012 · Viewed 23.9k times · Source

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! =)

Answer

iouri picture iouri · Oct 16, 2012

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