Ruby on Rails Form Helpers Creating a search form


To create a search form, enter the following code

<%= form_tag("/search", method: "get") do %>
  <%= label_tag(:q, "Search for:") %>
  <%= text_field_tag(:q) %>
  <%= submit_tag("Search") %>
<% end %>
  • form_tag: This is the default helper for creating a form. It's first parameter, /search is the action and the second parameter specifies the HTTP method. For search forms, it is important to always use the method get
  • label_tag: This helper creates an html <label> tag.
  • text_field_tag: This will create an input element with type text
  • submit_tag: This creates an input element with type submit