问题描述:

I have a form with multiple submit buttons called "select". I want to be able to tell which was clicked. I can't set the value to the number because I want them to say "select". How can I do this?

网友答案:

You can give them different name attributes instead of different value attributes. For example:

<%= f.submit "select", name: "select_one" %>
<%= f.submit "select", name: "select_two" %>

Clicking the first one will insert

"select_one"=>"select"

into your POST parameters and clicking the second one will insert

"select_two"=>"select"

into your POST parameters.

In your controller, simply use params[:select_one] and/or params[:select_two] as conditions in your logic, like so:

if params[:select_one]
  puts "select_one was clicked"
elsif params[:select_two]
  puts "select_two was clicked"
end
相关阅读:
Top