Section 3.3. Forms

3.3. Forms

So far we've been using helpers to generate links that request information from the server, but for really interesting applications, we'll want to send data to the server as well, and that means forms. First, we'll create a simple, non-Ajax form. The form_tag and end_form_tag helpers create an HTML form element. For example, this:

<%= form_tag :action => 'reverse' %> <%= end_form_tag %>

...generates this:

<form action="/chapter3/reverse" method="post"> </form>

3.3.1. Form Tag Helpers

Within a form, there are helpers to generate input fields. Here they are:

text_field_tag( name , value = nil , options = {} )

The keys in the options hash will be made into HTML attributes. For example:

<%= text_field_tag "name", "Scott",      :size     => 5,      :disabled => true,      :style    => "background-color: red" %>

The helper will produce this output:

<input type="text" name="name"  value="Scott"    size="5"   disabled="disabled"   style="background-color: red" />

hidden_field_tag( name , value = nil , options = {} )

Takes the same options as text_field_tag.

password_field_tag( name = "password" , value = nil , options = {} )

Takes the same options as text_field_tag.

file_field_tag( name , options = {} )

Takes the same options as text_field_tag.

check_box_tag( name , value = "1" , checked = false , options = {} )

Takes the same options as text_field_tag.

radio_button_tag( name , value , checked = false , options = {} )

Takes the same options as text_field_tag.

text_area_tag( name , content = nil , options = {} )

Takes the same options as text_field_tag, except that the :size option is a string specifying both the height and width of the text area. For example:

<%= text_area_tag "body", nil, :size => "25x10" %>

select_tag( name , option_tags = nil , options = {} )

Takes the same options as text_field_tag. option_tags is a string containing the options for the select box. For example:

<%= select_tag "people", "<option>Joe</option>" %>

Putting it all together, add a new form to index.rhtml view template:

<%= form_tag :action => 'reverse' %>   <p>Text to reverse: <%= text_field_tag 'text_to_reverse' %></p>   <p><%= submit_tag 'Reverse!' %></p> <%= end_form_tag %>

The form submits to the reverse action, which we already defined at the beginning of the chapter, but it still needs a template. Create it as app/views/chapter3/reverse.rhtml:

<%= @reversed_text %>

Now reload the page, and enter some dummy text, and submit the form (Figure 3-3). If all is well, the reverse action reverses the input string and renders the result on a new page (Figure 3-4).

Figure 3-3. Text field

Figure 3-4. Reversed text

3.3.2. Form Helpers

Form helpers (as opposed to the form tag helpers described in the previous section) are designed to help build forms that work with ActiveRecord objects that are assigned to the template from the controller. For example, suppose your template is assigned a @person object, which has a name attribute. To create a form field for the value, you'd use a Form Helper:

<%= text_field :person, :name %>

So instead of taking name and value arguments like form tag helpers, form helpers take object_name and method arguments. The available options are the same with both kinds of helper:

text_field( object_name , method , options = {} )
hidden_field( object_name , method , options = {} )
password_field( object_name , method , options = {} )
file_field( object_name , options = {} )
check_box( object_name , method , options = {} , checked_value = "1" , unchecked_value = "0" )
radio_button( object_name , method , tag_value , options = {} )
text_area( object_name , method , options = {} ) Using form_for

When you are creating forms to work with ActiveRecord objects, there's one other powerful helper: form_for. This helper is similar to form_tag, except that it's tied to a specific ActiveRecord object, and it creates a context for the form helper methods to run in, making form code far less verbose. It's generally preferable to use form_for instead of form_tag when working with ActiveRecord objects, because it helps you follow the DRY principle. Since ActiveRecord is outside the scope of this book, it won't be covered in detail here. See the API documentation for full details about form_for.

Ajax on Rails
Ajax on Rails
ISBN: 0596527446
EAN: 2147483647
Year: 2006
Pages: 103
Authors: Scott Raymond

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: