Basic Search Form in Rails
If you want to add a simple search page to your rails application, you’ll probably want a form that uses GET rather than POST, since GET will allow your visitors to bookmark the results page, and won’t complain if the user decides to refresh the results page.
Also, you’ll want the form to simply update the URL of the page itself, rather than have a controller parse the submitted data directly. This will allow you to grab the search parameters from the data in the URL and figure out what the return accordingly.
This is the code snippet you’re looking for:
<% form_tag(search_path, :method => "get") do %>
<%= text_field_tag(:s) %>
<%= submit_tag("Search", :name => nil) %>
<% end %>
In the form_tag, search_path is the named route you define in your config/routes.rb file.
text_field_tag(:s) will generate the following input field HTML:
<input id="s" name="s" type="text" />
When submitted, whatever the user enters into this field will be appended to the url in the form ?s=SEARCHTERM.
Also, notice that :name => nil is added to the submit_tag. If it were omitted, then the resulting URL would be search?s=searchterm&commit=Search

