When making a ruby on rails application you will most likely want to get input from a user to make new, or change existing instances of models in your app, or input search parameters, or a variety of other things. Writing out the HTML for all the aspects of a form, and getting it to send to the action you need, like post or patch, is very tedious and leaves room to make a lot of errors. Thankfully, rails has form helpers to make the process easier: form_tag and form_for.
The form_tag helper can be used to create any type of form and it takes care of generating the basic framework of an HTML form, including creating a hidden field with an authenticity token needed to prevent CSRF (where another site can hack the form you are working with). You can pass it a hash to tell it what controller to send it to, an action in the controller to use, and a method such as POST or GET. Form_tag also has a number of helpers for generating form elements, such as
label_tag , and many others.You just need to know what arguments each helper takes, and the form_tag will generate the HTML input tags with the correct types and names to pass back in the params hash to the controller. Here’s an example:
<%= form_tag("/search", method: "get") do %>
<%= label_tag(:q, "Search for:") %>
<%= text_field_tag(:q) %>
<%= submit_tag("Search") %>
<% end %>
In comparison, form_for is designed to be used on an instance of an object. For example, when you pass the instance to the form_for it can look at the instance and figure out whether you are creating a new instance, so it will POST to the create method in the controller, or whether you are editing an existing instance, so it will PUT or PATCH to the update method. Here’s an example:
<%= form_for @article do |f| %>
<%= f.label :title %>
<%= f.text_field :title %>
<%= f.text_area :body, size: "60x12" %>
<%= f.submit "Create" %>
<% end %>
The form_for helper creates a FormBuilder object, which is the f in the example above. FormBuilder has a variety of methods you can call on it that work similarly to the helpers for the form_tag. It also handles the keys sent to the params hash so that all of the attributes are nested in a hash with the key the name of the instance you are working with, like
article =>[:title, :body] which is necessary for using strong params. Another benefit is that the form for creating and editing an object is exactly the same, so if you use a partial template, you only have to write one form.
Ultimately, form helpers are faster than having to write out all of the HTML by hand, and they can be used to create all different kinds of forms. The main thing to think about when figuring out which helper to use is mostly based on the fact that form_for is much better for forms that interact with instances of objects. You can use a form_tag, but you have to clearly and carefully define the names so that the params hash is formatted correctly and you have to specify the method and action for where to send it. In conclusion, both form_tag and form_for have their own uses that make generating HTML forms much easier.