RubyOnRails: Forms 102
As I mentioned in a previous article (Forms 101), I had some difficulties coming to RubyOnRails from an asp.net background seeing as certain things that were non-existent in asp.net were rather important in Rails. I am referring to the the controls contained within the html form tags. There was only one set in asp.net that a person had to become familiar with to be able to make things happen. In Rails there is a few sets of methods that render out the desired controls and each set has a purpose.
If you are not familiar with the various form blocks that exist within the Rails framework I suggest you give the Forms 101 article a read first and then come back to this one. This topic is not a complex one, but when I was learning I could never find a single source of information that listed all the form control methods, so that is where this comes in handy.
There are common names used throughout all the various method types that you may or may not already be aware of. Below are a list of the common method names.
- text_field
- text_area
- select
- checkbox
- radio_button
There are, to my knowledge, three types of methods that exist that are based on the above names.
The first one is to append the _tag suffix to the method name, ex. text_field_tag, checkbox_tag. Each of these methods requires one argument, the parameter’s name. In the example it should be noted that the id and name of the control match the parameter name.
<%= text_field_tag :first_name %> #will render to the web browser as <input type="text" id="first_name" name="first_name"></input>
The big difference with this method is that on the server side the form values are accessed directly from the params[] array, ie params[:first_name], params[:last_name]. Having the form values stored like this makes it easy to quickly access the values and pass them to a method call, ex. a login authentication method (Member.valid(params[:user_name], params[:password]))
The second type consists of names that match the method names that were listed above, ex text_field, checkbox, radio_button. This method requires two arguments, where the first is the model name and the second is the parameter name.
<%= text_field :member, :first_name %> #will render to the web browser as <input type="text" id="member_first_name" name="member[first_name]"></input>
The key things to note are the values of the id and name attributes of the input tag where the model name is now included in each. So what does this do? The name parameter allows the request object to hold all the form parameters in its own sub-array that can later be accessed through the params array, ex. params[:member][:first_name]. The benefit to this is that it makes it easy to fill a model with all the form values as shown below.
def create @member = Member.new(params[:member]) end
The last method type is more geared to be used within the form_for block. They are much like the previous type mentioned except they are called through the block’s parameter and only require one parameter name although the html output will be the same. The reason for this is the form_for’s block parameter that the text_field method is called from. This block parameter will create the corresponding form control that matches whatever value follows the form_for, ex. in this case :member.
<% form_for :member, :url => member_url() do |form| %> form.text_field :first_name <% end %> #will render to the web browser as <input type="text" id="member_first_name" name="member[first_name]"></input>
The main benefit of this type is that it prevents you from having to define the model that the form control is for for each form control, and less is better.
Now that you have a good idea of what each type is for, it is time for the key point. You can, for the most part, mix the the types of form methods that reside within the form tags. Although the form.text_field etc must only exist within a form_for, all the others can exist whenever and wherever. This then allows you to easily obtain data through the form that does not match the model without too much effort.
Just to top things off, below is a bunch of common form methods that a person should be familiar with, although I am sure there is a few that I am missing. Hopefully this may clear up thing for some, and serve as a reminder for others.
<% form_tag do %> Text Field Tag: <%= text_field_tag :task, :title %> Text Field <%= text_field :task, :title, :size => 30 %> <br> Text Area: <%= text_area :task, :title, :rows => 5, :columns => 30 %> <br> Select with simple array: <%= options = [1,2,3,4,5,6,7,8,9] select :task, :points, options %> <br> Select with hash table: <%= options = {:one => "First", :two => "Second", :three => "Last"} select :task, :points, options %> <br> Select control bound to list of models: <%= #create some tasks if Task.count == 0 ["Learn RubyOnRails", "Create practice site", "Create real world site"].each do |title| Task.create(:title => title, :points => 10) end end tasks = Task.find :all #select control must be bound to list of 1x2 array or hash select :task, :points, tasks.map{ |t| [t.title, t.points] }, :prompt => "Select..." %> <br> Another select bound to a model: <%= collection_select(:task, :points, tasks, :id, :title) %> <br> Checkboxes: <%= check_box :task, :complete, {:style => "color: red; font-size: 20px;"}, "yes", "no" %> Is Complete (yes no) <%= check_box :task, :complete, {:style => "color: red; font-size: 20px;"} %> Is Complete (1 0) <br> Radio buttons: <%= radio_button :task, :complete, "complete" %> Complete <%= radio_button :task, :complete, "incomplete" %> InComplete <br> Date Select Helpers: <!-- http://api.rubyonrails.org/classes/ActionView/Helpers/DateHelper.html#M000577 --> defaults to the current date: <%= date_select(:task, :completed_on) %> <br> Setting the min and max value within in the list: <%= date_select(:task, :completed_on, :start_year => 1970, :end_year => 2000) %> <br> Hidden Field(seriously there is on here): <%= hidden_field :task, :title %> <br> Password: <%= password_field :task, :title %> <br> Password Check: <%= password_field :task, :title_confirmation %> <br> File: <%= file_field :member, :picture %> <br> <%= submit_tag "Submit this form" %> <% end %>
Trackbacks
Use this link to trackback from your own site.

[...] chrisolsen.org » Blog Archive » RubyOnRails: Forms 102 Fri, 24 Aug 2007 23:43:24 MDT [...]
The link to Forms 101 goes to a page that says this:
The page you were looking for doesn’t exist.
You may have mistyped the address or the page may have moved.
I had changed the blog to a sub-domain. The links are fixed now.
Thanks for letting me know.