Sunday, March 9, 2008

How to disable submit button parameter in Rails

Here's a quick tip for Rails I've just learned from Railscast #37: Simple Search Form: when you submit a form in Rails, submit button parameter gets passed by default and it can be kinda annoying when you don't need it. For example by submitting this form:
1
2
3
4
<% form_tag( '/search', :method => 'get' ) do %>
  <%= text_field_tag :search_str %>
  <%= submit_tag "Search" %>
<% end %>
You get following parameters in URL: ?search_str=something;commit=Search

Let's say we don't need the commit=Search parameter. Fortunately, it's
really easy to get rid of it. Default name of submit_tag's parameter is "commit" and it's set by :name key. If you set it to nil, the parameter won't get pass. So just change the submit_tag helper to this:
<%= submit_tag "Search", :name => nil %>
And you get following parameters when submitting: ?search_str=something

6 comments:

Anonymous said...

Gratsi

Unknown said...

Thanks!

Tejuteju said...

Excellent article. Very interesting to read. I really love to read such a nice article. Thanks! keep rocking.
Ruby on Rails Online Course

maha said...

Great Article… I love to read your articles because your writing style is too good, its is very very helpful for all of us and I never get bored while reading your article because

Digital Marketing Training in Chennai

Digital Marketing Course in Chennai


Blck Luxury car said...


BLCK Luxury - Bangalore | Luxury Car Rental Bangalore | Luxury Taxi Bangalore | Self Drive Cars in Bangalore

Luxury Car Rental Bangalore

Anonymous said...


WhyDonate is één van de populairste crowdfunding platforms van alle Europese landen en de Verenigde Staten
WhyDonate is niet alleen geschikt voor het opzetten van fondsenwervende campagnes, maar bevat ook vele actieve fondsenwervers waaraan men kan doneren.
Top 10 crowdfunding platforms in België

Crowdfunding Platfoarm