Stopping a submit_tag from Sending it's Value
12 Dec 2007
OK, so say you have a form as follows:
<% form_tag '/', :method => :get do -%>
<p><%= text_field_tag 'q', params[:q] ||= '' %> <%= submit_tag 'Search' %>
<% end %>
That form will send the contents of the text box to the page at ‘/’. Unfortunately, it will also send through the value of the submit button, which is ‘Search’ in this case. The url generated will be:
/?q=something&commit=Search
In this case, we would want only the ‘q’ parameter passing through, and in order to do this, all we need do is nullify the name parameter:
<% form_tag '/', :method => :get do -%>
<p><%= text_field_tag 'q', params[:q] ||= '' %> <%= submit_tag 'Search', :name => nil %>
<% end %>
This will create the following url on submission:
/?q=something