4

I have two submit_tag on my form and I would like to send a different parameter on each. How can I do this?

My form view:

<%= form_tag(some_path, :method => "get") do %>
   <%= text_field_tag :number %>
   <%= text_field_tag :name %>
   <%= submit_tag "Op01", class: "btn_search", my_parameter: 1 %>
   <%= submit_tag "Op02", class: "btn_search", my_parameter: 2 %>
<% end %>

And on my controller:

@oper_type = params[:my_parameter]

But when I display the @oper_type it is always nil.

Thanks.

Cleiviane
  • 51
  • 1
  • 5

2 Answers2

8
<%= submit_tag "Op01", class: "btn_search", value: 1 %>
<%= submit_tag "Op01", class: "btn_search", value: 2 %>

@oper_type = params[:commit] # 1 or 2

or a little simplier

<%= submit_tag "Op01", class: "btn_search" %>
<%= submit_tag "Op01", class: "btn_search" %>

@oper_type = params[:commit] # "Op01" or "Op02"
megas
  • 21,401
  • 12
  • 79
  • 130
  • Maybe the answer is out of date as I needed to use something like: `submit_tag 'Send', :name => 'MyParameter'` – brod Jun 06 '17 at 16:21
1

Is there a way to accomplish this without revealing the value to the user on the submit button? Say you want to have a different text on the button and a hidden value sent to the controller...

Jon Wexler
  • 151
  • 9
  • I'd also like to do the same thing but can't find a solution in documentation. It's got to be answered somewhere... – ThinQtv Aug 27 '17 at 02:08