rails link_to remote with params

2019-05-11 20:30发布

问题:

I'd like to trigger a remote action for a model using a link. Basically all this link needs to do is trigger a method with one parameter.

Here's my code:

= link_to 'Move Up', reorder_collection_folder_path(@collection, folder), :reorder => :up, :remote => true

This does trigger the Folders#reorder controller action as expected, but the :reorder param is not being passed through. My log says:

Started GET "/collections/1/folders/1/reorder" for 127.0.0.1 at 2011-03-01 18:03:31 -0600
  Processing by FoldersController#reorder as JS
  Parameters: {"collection_id"=>"1", "id"=>"1"}

So, how can I pass a parameter through a remote link? What am I doing wrong here?

回答1:

Found the solution.

For remote links the code should be:

= link_to 'Move Up', reorder_collection_folder_path(@collection, folder, :reorder=>:up), :remote => true

IE the params need to go inside the path helper.