Problem with link_to_remote in rails

I have a consistency issue using link_to_remote in rails.

I have 2 use cases for link_to_remote and they generate different ajax. I can’t understand why, and it drives me crazy.

Here is an example use one ...

<%= link_to_remote "section-", :update => "sections", :url => {:action => :destroy, :controller => "sections", :id => @section.id } %> 

This creates the appropriate ajax (as shown below) and works as I expect. Note that it raises the: action parameter from the call and correctly inserts it into ajax.

 <a href="#" onclick="if { new Ajax.Updater('sections', '/sections/destroy/1', {asynchronous:true, evalScripts:true, parameters:'authenticity_token=' + encodeURIComponent('f5e50e62fafd118e4588b33c9571ea6eef864176')}); }; return false;">section-</a> 

I also have another instance where I use link_to_remote, but it generates the wrong ajax. The use case is almost identical, except that the controller is different. In any case, I would not expect this to lead to different ajax.

Call...

 <%= link_to_remote "question-", :update =>"questions-1", :url => {:action => :destroy, :controller => "questions", :id => @question.id} %> 

The resulting ajax ...

 <a href="#" onclick="if { new Ajax.Updater('questions-1', '/questions/1', {asynchronous:true, evalScripts:true, parameters:'authenticity_token=' + encodeURIComponent('f5e50e62fafd118e4588b33c9571ea6eef864176')}); }; return false;">question-</a> 

The obvious difference here is in the second argument of Ajax.Updater. The: action parameter is missing from this path. What for? This leads to a broken code for me, but I cannot understand why this is happening. The calls to link_to_remote are almost identical.

Please point me in the right direction. Thanks.

Below is my route.rb file ...

 ActionController::Routing::Routes.draw do |map| map.resources :questions, :has_one => :section, :collection => { :sort => :post } map.resources :sections, :has_many => :questions, :has_one => :form, :collection => { :sort => :post } map.resources :forms, :has_many => :sections # You can have the root of your site routed with map.root -- just remember to delete public/index.html. map.root :controller => "forms" map.connect ':controller/:action/:id' map.connect ':controller/:action/:id.:format' end 
+4
source share
2 answers

Just adding a method: method =>: delete to your link_to_remote call might be the easiest solution for you:

 <%= link_to_remote "question-", :update =>"questions-1", :url => {:action => :destroy, :controller => "questions", :id => @question.id}, :method => :delete %> 

This should force the call to / questions /: id to use the HTTP DELETE method. If you want the above call to generate url / questions / destroy /: id, I suppose you will need to manually change the routes, since by default map.resources does not seem to achieve this result.

+2
source

What will you get if you copy this into your opinion?

 <%= url_for :action => :destroy, :controller => :questions, :id => @question.id %> 

I suspect the problem is not with link_to_remote, but with routing . Once url_for returns the URL you expect, try link_to_remote again.

+3
source

All Articles