简体   繁体   中英

How can I pass an extra parameter to a link_to remote?

Given the following:

<%= link_to "Add", group_request_path(gr), :confirm => "Are you sure?", :method=> :put, :remote => true %>

<%= link_to "Ignore", group_request_path(gr), :confirm => "Are you sure?", :method=> :put, :remote => true %>

How can I pass a decision param with either add or ignore in the link_to 's above? Is there an option to add a URL param somehow with a link_to? I don't want to have to use forms.

the *_path helpers accept a hash of options to append to the path as a query string.

In your example, assuming group_request_path(gr) outputs something like /group_requests/123 , you could add a query string with add/ignore actions like this:

group_request_path(gr, :action => 'add') # /groups_requests/123?action=add

or

group_request_path(gr, :add => 1) # /groups_requests/123?add=1

However this probably isn't the correct way of going about this. It looks like you should probably have distinct actions in your controller, and routes specific to adding or ignoring. You'll have to add something like the following to config/routes.rb :

put 'group_requests/:id/add'    => 'groups_requests#add',    :as => :add_group_request
put 'group_requests/:id/ignore' => 'groups_requests#ignore', :as => :ignore_group_request

Your links then become

<%= link_to "Add", add_group_request_path(gr), :confirm => "Are you sure?", :method=> :put, :remote => true %>

<%= link_to "Ignore", ignore_group_request_path(gr), :confirm => "Are you sure?", :method=> :put, :remote => true %>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM