Rails controller management without viewing

I just want to make rails 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 

and do it

 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 that (but this does not apply to me, I want to get feedback for the user through json and through 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 can I tell the rails that this action in the PictureController needs no introduction?

Solved!

I really did not solve the problem of transferring rails, I do not need a view, I just created another controller, put a method in it and said that the routing of the routes corresponds to an unfriendly action with a match call. I can’t say for sure, but I think this is a problem with resources :picture in my routes.rb ...

But anyway, thank you guys! =)

+8
ruby-on-rails rails-routing
source share
3 answers

Just created another controller with hostility:

 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 .rb routes to fit this action:

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

and now my link is not like:

 <%= link_to 'Dislike!', {:action => :dislike, :controller => :likes}, :method => :post %> 
+4
source share

Something like that?

 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 
+24
source share

Most likely the problem is that you are doing this action with an ajax request. Thus, the controller is looking for format.js , but you did not specify the answer for this format in your block. Thus, it drops to the default value.

Try

 format.js { render json: @picture } 

You may also need to specify an ajax request to wait for a json response.

0
source share

All Articles