RubyOnRails: Forms 101
Coming from an asp.net background creating forms was pretty simple, mainly because whenever a new .aspx page was created the form tag was automatically added to the new page, so a person really didn’t ever have to create a form tag.
In RubyOnRails you have a few more options. To demonstrate this I have create 5 examples, each of which renders a form that can be used to create a new task. The differences are highlighted in red to make things a little more visible.
<h3>#Example 1</h3> <%= start_form_tag :controller => :tasks, : action => :create %> <%= text_field_tag :title, params[:title] %> <%= end_form_tag %> <h3>#Example 2</h3> <% form_tag :controller => :tasks, : action => :create do %> <%= text_field_tag :title, params[:title] %> <% end %> <h3>#Example 3</h3> <% form_tag :controller => :tasks, : action => :create do %> <%= >text_field :task, :title %> <% end %> <h3>#Example 4</h3> <% form_for :task, :url => {:controller => :tasks, : action => :create} do |t| %> <%= t.text_field :title %> <% end %>
The first example is now deprecated so it is pretty safe to avoid that one, so there is not much use in going over it any further.
The only difference with Example 2 is the removal of the deprecated end_form_tag and containing the form within a block as can be seen by noticing the do on the end of the first line. If you look at the rendered html output it can be seen the names and ids of the form elements match that of the param name that was used, in this case :title.
<form action="/tasks/create" method="post"> <input id="title" name="title" type="text" /> </form>
Using form controls with the _tag postfix are useful when the data contained within a form does not correspond to a model. One example of this would be a login form containing a username and password. Another example could be a form that emails the admin comments by users.
Not having the data match a model also means that the form data that is submitted to the server is accessed through the params array using a key matching the name of the form element
def create title = params[:title] ... end
In example number 3 the text_field method is used instead of the text_field_tag. If you compare the parameters that are passed to each of the methods, the first parameter to the text_field method is the name of the model. If you then examine the rendered html the id and the name are prefixed by the model name.
<form action="/tasks/create" method="post"> <input id="task_title" name="task[title]" size="30" type="text" /> </form>
When accessing the form values on the server side, instead of having to access them individually, they are now contained within a sub-array accessed by the key which is the value of the first parameter passed to the text_field method.
The benefit of creating a form like example 3 is that it allows for the form to contain data belonging to more than one model. So lets say your program wants to be able to keep track of a person’s address history so you will be keeping this information in a separate table to the person’s personal info. You could then obtain the person’s name, password, etc as well as their current address within one form.
<% form_tag :controller => :tasks, :action => :create do %> First Name: <%= text_field :user, :first_name %> Last Name: <%= text_field :user, :last_name %> Address 1: <%= text_field :address, :address_1 %> ... <% end %> #controller code def create user = User.new(params[:user]) user.addresses << Address.new(params[:address]) user.save! end
The last example uses the form_for method to generate the form tags. The first parameter passed to the method is a reference to model that was created on the server within, if you are using REST, either the new or edit methods. The main difference with the form_for method are the methods that are called within the form block, which is discussed in another article.
This was a topic of confusion when I first started reading up on Rails, so hopefully this will clear things up for others that are new to the framework.
Trackbacks
Use this link to trackback from your own site.

[...] I previously mentioned in a previous article (Forms 101), I had some difficulties coming to RubyOnRails from an asp.net background seeing as certain things [...]
Very useful !! thanks a lot. [good tutorials about rails are not many, no ?]
[...] I mentioned in a previous article (Forms 101), I had some difficulties coming to RubyOnRails from an asp.net background seeing as certain things [...]