Action View: Forms

25Sep09

(Notes from Chapter 23 of “Agile Web Development with Rails, Third Edition”, sections 23.4 to 23.7)

Intro

After submission, Rails constructs the params hash with the form values.

  • the id field is extracted using the routing methods from the form action
  • other simple values (i.e. not having a bracket in their name) are stored as scalars in the hash
  • if the name has brackets in it, a hash is constructed to hold the values with the string inside the brackets as the key (can be nested)

Model Forms

  • use the form_to helper – inside <% … %> not <%= … %> like this:
    <% form_for :group do |form| %>

    <% end %>
    where :group represents 1) the name of the class we’re using, group, as well as 2) the name of the instance variable that we’re looking at,  @group
  • some options on form_to include:
    • :url is a hash taking the same arguments as you can use in url_for – if this is not specified, then it defaults to the current action
    • :html is a hash of HTML attributes on the generated form tag, such as :class
  • form_for takes a block and passes it a form builder object (that form between the | and | above)
    • use its helpers to build the input elements
    • all form builder methods minimally take the name of the attribute in the model to be queried as a parameter
      • e.g. <%= form.text_field :name %>
    • form.text_field(:attribute, options)
      • common options are :size, :maxlength
    • form.hidden_field(:attribute, options)
    • form.password_field(:attribute, options)
    • form.text_area(:attribute, options)
      • common options are :cols, :rows
    • form.radio_button(:attribute, tag_value, options)
    • form.check_box(:attribute, options, on_value, off_value)
      • on_value defaults to “1” and off_value defaults to “0”
      • Rails generates a hidden field with the same name as the checkbox to ensure that the field is passed in the request regardless of whether it is checked
    • form.select(:attribute, choices, options, html_options)
      • choices populates the selection list (its type can be any enumerable object – arrays, hashes, database query results)
        • array of strings, with each element being both the value and the display text
        • if each element is an array, the first value is the display text and the last value is the value
        • if choices is a hash, its keys are the display text and its values are the values of the list
      • form.collection_select(:attribute, collection, :value_attribute, :display_attribute)
        • takes a collection of objects in collection, whose value and display text attributes are indicated
    • form.date_select(:attribute, options)
    • form.datetime_select(:attribute, options)
    • form.label(:object, :method, ‘text’, options)
      • :object assigned to the template
      • :method specified attribute
  • fields_for helps with capturing information on multiple models
  • Errors – interacting with the errors structure in each model
    • error_message_on(:instance_name, :attribute) – returns the error text associated with a particular field
    • error_message_for(:instance_name) – displays a box at the top of the form showing all errors, highlights the fields in error
  • Custom form builders, i.e. extending the default behaviour of the Rails class FormBuilder are described in pp. 534-8

Non-Model Forms

  • FormTagHelper
  • all of the method names end in _tag
  • use form_tag instead of form_to, with the following parameters:
    1. :action – a hash taking the same options as url_for
    2. hash with additional form attributes
  • since we’re not using a builder, it looks like this:
    <% form_tag :action => ‘save’ do %>

    <% end %>
  • instead of using the error_message_ functions, can loop through the @errors instance variable
Advertisements


No Responses Yet to “Action View: Forms”

  1. Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: