Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails controller action without a view

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

like image 348
Crystian Leão Avatar asked Oct 16 '12 16:10

Crystian Leão


People also ask

How should you use filters in controllers?

Filters are inherited, so if you set a filter on ApplicationController , it will be run on every controller in your application. The method simply stores an error message in the flash and redirects to the login form if the user is not logged in. If a "before" filter renders or redirects, the action will not run.

What does Before_action do in Rails?

When writing controllers in Ruby on rails, using before_action (used to be called before_filter in earlier versions) is your bread-and-butter for structuring your business logic in a useful way. It's what you want to use to "prepare" the data necessary before the action executes.

What is the role of rails controller?

The Rails controller is the logical center of your application. It coordinates the interaction between the user, the views, and the model. The controller is also a home to a number of important ancillary services. It is responsible for routing external requests to internal actions.


1 Answers

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
like image 166
iouri Avatar answered Sep 27 '22 20:09

iouri