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?
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! =)
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With