<p>Platforms: <%= check_box_tag("platforms[]","Linux") %> Linux <%= check_box_tag("platforms[]","Mac OSX") %> Mac OSX <%= check_box_tag("platforms[]","Windows") %> Windows</p>
<%= submit_tag("Save Data") %> <% end %>
When the form has been submitted, you can access the data using the params hash within your controller.
app/controllers/data_controller.rb:
class DataController < ApplicationController
def enter end
def show_data @name = params[:name] @tools = params[:tools] || [] @platforms = params[:platforms] || [] end end
Figure 4-1. A web form containing several HTML input elements
Discussion
The web server stores the elements of a submitted form in the request object. These elements are available to your application through the params hash. The params hash is unique because you can access its elements using strings or symbols as keys.
Figure 4-1 shows the form; it has three different types of HTML elements.
The following view displays the data that the form collects. The last line is a call to the debug template helper, which displays the contents of the params hash in yaml format:
Figure 4-2. Form data displayed in a view with additional debugging output
To access the name field of the form, use :name as the key to the params hash (e.g., params [:name]). The selected elements of the multiselect list and the checkboxes are stored in the params hash as arrays named after their associated HTML element names.
For example, if you submit the form in the solution with Python and Ruby selected for Tools and Mac OS X checked for Platforms, the params hash contains the following arrays:
This behavior is triggered by appending [] to the name of an element that can have more than one value. If no items are selected, there will be no variable in params corresponding to that element.
Form data can also be structured as an arbitrarily deeply nested tree of hashes and arrays within the params hash. Hashes are created by placing the name of the nested hash between the square brackets at the end of the field name. The following hidden form fields illustrate a nesting that is up to three levels deep (i.e., params contains a student hash, which contains a scores hash, which contains a :midterm array with values and :final key with a value).