aboutsummaryrefslogtreecommitdiffstats
path: root/railties/doc/guides/html/form_helpers.html
diff options
context:
space:
mode:
Diffstat (limited to 'railties/doc/guides/html/form_helpers.html')
-rw-r--r--railties/doc/guides/html/form_helpers.html206
1 files changed, 151 insertions, 55 deletions
diff --git a/railties/doc/guides/html/form_helpers.html b/railties/doc/guides/html/form_helpers.html
index 7ff4a13a6a..134679b9f2 100644
--- a/railties/doc/guides/html/form_helpers.html
+++ b/railties/doc/guides/html/form_helpers.html
@@ -226,7 +226,23 @@ ul#navMain {
<li><a href="#_the_select_tag_and_options">The select tag and options</a></li>
- <li><a href="#_select_boxes_for_dealing_with_models">Select boxes for dealing with models</a></li>
+ <li><a href="#_select_helpers_for_dealing_with_models">Select helpers for dealing with models</a></li>
+
+ <li><a href="#_option_tags_from_a_collection_of_arbitrary_objects">Option tags from a collection of arbitrary objects</a></li>
+
+ <li><a href="#_time_zone_and_country_select">Time zone and country select</a></li>
+
+ </ul>
+ </li>
+ <li>
+ <a href="#_miscellaneous">Miscellaneous</a>
+ <ul>
+
+ <li><a href="#_file_upload_form">File upload form</a></li>
+
+ <li><a href="#_scoping_out_form_controls_with_tt_fields_for_tt">Scoping out form controls with <tt>fields_for</tt></a></li>
+
+ <li><a href="#_making_custom_form_builders">Making custom form builders</a></li>
</ul>
</li>
@@ -237,9 +253,9 @@ ul#navMain {
<h1>Rails form helpers</h1>
<div id="preamble">
<div class="sectionbody">
-<div class="para"><p>Forms in web applications are an essential interface for user input. However, form markup can quickly become tedious to write and maintain because of form control naming and their numerous attributes. Rails deals away with these complexities by providing view helpers for generating form markup. However, since they have different use-cases, developers are required to know all the differences between similar helper methods before putting them to use.</p></div>
-<div class="para"><p>In this guide we will:</p></div>
-<div class="ilist"><ul>
+<div class="paragraph"><p>Forms in web applications are an essential interface for user input. However, form markup can quickly become tedious to write and maintain because of form control naming and their numerous attributes. Rails deals away with these complexities by providing view helpers for generating form markup. However, since they have different use-cases, developers are required to know all the differences between similar helper methods before putting them to use.</p></div>
+<div class="paragraph"><p>In this guide we will:</p></div>
+<div class="ulist"><ul>
<li>
<p>
Create search forms and similar kind of generic forms not representing any specific model in your application;
@@ -278,16 +294,16 @@ Build complex, multi-model forms.
</div>
<h2 id="_basic_forms">1. Basic forms</h2>
<div class="sectionbody">
-<div class="para"><p>The most basic form helper is <tt>form_tag</tt>.</p></div>
+<div class="paragraph"><p>The most basic form helper is <tt>form_tag</tt>.</p></div>
<div class="listingblock">
<div class="content">
<pre><tt>&lt;% form_tag do %&gt;
Form contents
&lt;% end %&gt;</tt></pre>
</div></div>
-<div class="para"><p>When called without arguments like this, it creates a form element that has the current page for action attribute and "POST" as method (some line breaks added for readability):</p></div>
+<div class="paragraph"><p>When called without arguments like this, it creates a form element that has the current page for action attribute and "POST" as method (some line breaks added for readability):</p></div>
<div class="listingblock">
-<div class="title">Example: Sample rendering of <tt>form_tag</tt></div>
+<div class="title">Sample rendering of <tt>form_tag</tt></div>
<div class="content">
<pre><tt>&lt;form action="/home/index" method="post"&gt;
&lt;div style="margin:0;padding:0"&gt;
@@ -296,7 +312,7 @@ Build complex, multi-model forms.
Form contents
&lt;/form&gt;</tt></pre>
</div></div>
-<div class="para"><p>If you carefully observe this output, you can see that the helper generated something we didn't specify: a <tt>div</tt> element with a hidden input inside. This is a security feature of Rails called <strong>cross-site request forgery protection</strong> and form helpers generate it for every form which action isn't "GET" (provided that this security feature is enabled).</p></div>
+<div class="paragraph"><p>If you carefully observe this output, you can see that the helper generated something we didn&#8217;t specify: a <tt>div</tt> element with a hidden input inside. This is a security feature of Rails called <strong>cross-site request forgery protection</strong> and form helpers generate it for every form which action isn&#8217;t "GET" (provided that this security feature is enabled).</p></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
@@ -306,7 +322,7 @@ Build complex, multi-model forms.
</tr></table>
</div>
<h3 id="_generic_search_form">1.1. Generic search form</h3>
-<div class="para"><p>Probably the most minimal form often seen on the web is a search form with a single text input for search terms. This form consists of:</p></div>
+<div class="paragraph"><p>Probably the most minimal form often seen on the web is a search form with a single text input for search terms. This form consists of:</p></div>
<div class="olist"><ol>
<li>
<p>
@@ -337,9 +353,9 @@ a submit element.
<td class="content">Always use "GET" as the method for search forms. Benefits are many: users are able to bookmark a specific search and get back to it; browsers cache results of "GET" requests, but not "POST"; and other.</td>
</tr></table>
</div>
-<div class="para"><p>To create that, we will use <tt>form_tag</tt>, <tt>label_tag</tt>, <tt>text_field_tag</tt> and <tt>submit_tag</tt>, respectively.</p></div>
+<div class="paragraph"><p>To create that, we will use <tt>form_tag</tt>, <tt>label_tag</tt>, <tt>text_field_tag</tt> and <tt>submit_tag</tt>, respectively.</p></div>
<div class="listingblock">
-<div class="title">Example: A basic search form</div>
+<div class="title">A basic search form</div>
<div class="content">
<pre><tt>&lt;% form_tag(search_path, :method =&gt; "get") do %&gt;
&lt;%= label_tag(:q, "Search for:") %&gt;
@@ -353,7 +369,7 @@ a submit element.
<img src="./images/icons/tip.png" alt="Tip" />
</td>
<td class="content">
-<div class="para"><p><tt>search_path</tt> can be a named route specified in "routes.rb":</p></div>
+<div class="paragraph"><p><tt>search_path</tt> can be a named route specified in "routes.rb":</p></div>
<div class="listingblock">
<div class="content">
<pre><tt>map.search "search", :controller =&gt; "search"</tt></pre>
@@ -361,9 +377,9 @@ a submit element.
</td>
</tr></table>
</div>
-<div class="para"><p>The above view code will result in the following markup:</p></div>
+<div class="paragraph"><p>The above view code will result in the following markup:</p></div>
<div class="listingblock">
-<div class="title">Example: Search form HTML</div>
+<div class="title">Search form HTML</div>
<div class="content">
<pre><tt>&lt;form action="/search" method="get"&gt;
&lt;label for="q"&gt;Search for:&lt;/label&gt;
@@ -371,7 +387,7 @@ a submit element.
&lt;input name="commit" type="submit" value="Search" /&gt;
&lt;/form&gt;</tt></pre>
</div></div>
-<div class="para"><p>Besides <tt>text_field_tag</tt> and <tt>submit_tag</tt>, there is a similar helper for <em>every</em> form control in HTML.</p></div>
+<div class="paragraph"><p>Besides <tt>text_field_tag</tt> and <tt>submit_tag</tt>, there is a similar helper for <em>every</em> form control in HTML.</p></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
@@ -381,22 +397,22 @@ a submit element.
</tr></table>
</div>
<h3 id="_multiple_hashes_in_form_helper_attributes">1.2. Multiple hashes in form helper attributes</h3>
-<div class="para"><p>By now we've seen that the <tt>form_tag</tt> helper accepts 2 arguments: the path for the action attribute and an options hash for parameters (like <tt>:method</tt>).</p></div>
-<div class="para"><p>Identical to the <tt>link_to</tt> helper, the path argument doesn't have to be given as string or a named route. It can be a hash of URL parameters that Rails' routing mechanism will turn into a valid URL. Still, we cannot simply write this:</p></div>
+<div class="paragraph"><p>By now we&#8217;ve seen that the <tt>form_tag</tt> helper accepts 2 arguments: the path for the action attribute and an options hash for parameters (like <tt>:method</tt>).</p></div>
+<div class="paragraph"><p>Identical to the &#8216;link_to` helper, the path argument doesn&#8217;t have to be given as string or a named route. It can be a hash of URL parameters that Rails&#8217; routing mechanism will turn into a valid URL. Still, we cannot simply write this:</p></div>
<div class="listingblock">
-<div class="title">Example: A bad way to pass multiple hashes as method arguments</div>
+<div class="title">A bad way to pass multiple hashes as method arguments</div>
<div class="content">
<pre><tt>form_tag(:controller =&gt; "people", :action =&gt; "search", :method =&gt; "get")
# =&gt; &lt;form action="/people/search?method=get" method="post"&gt;</tt></pre>
</div></div>
-<div class="para"><p>Here we wanted to pass two hashes, but the Ruby interpreter sees only one hash, so Rails will construct a URL that we didn't want. The solution is to delimit the first hash (or both hashes) with curly brackets:</p></div>
+<div class="paragraph"><p>Here we wanted to pass two hashes, but the Ruby interpreter sees only one hash, so Rails will construct a URL that we didn&#8217;t want. The solution is to delimit the first hash (or both hashes) with curly brackets:</p></div>
<div class="listingblock">
-<div class="title">Example: The correct way of passing multiple hashes as arguments</div>
+<div class="title">The correct way of passing multiple hashes as arguments</div>
<div class="content">
<pre><tt>form_tag({:controller =&gt; "people", :action =&gt; "search"}, :method =&gt; "get")
# =&gt; &lt;form action="/people/search" method="get"&gt;</tt></pre>
</div></div>
-<div class="para"><p>This is a common pitfall when using form helpers, since many of them accept multiple hashes. So in future, if a helper produces unexpected output, make sure that you have delimited the hash parameters properly.</p></div>
+<div class="paragraph"><p>This is a common pitfall when using form helpers, since many of them accept multiple hashes. So in future, if a helper produces unexpected output, make sure that you have delimited the hash parameters properly.</p></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
@@ -406,7 +422,7 @@ a submit element.
</tr></table>
</div>
<h3 id="_checkboxes_radio_buttons_and_other_controls">1.3. Checkboxes, radio buttons and other controls</h3>
-<div class="para"><p>Checkboxes are form controls that give the user a set of options they can enable or disable:</p></div>
+<div class="paragraph"><p>Checkboxes are form controls that give the user a set of options they can enable or disable:</p></div>
<div class="listingblock">
<div class="content">
<pre><tt>&lt;%= check_box_tag(:pet_dog) %&gt;
@@ -421,7 +437,7 @@ output:
&lt;input id="pet_cat" name="pet_cat" type="checkbox" value="1" /&gt;
&lt;label for="pet_cat"&gt;I own a cat&lt;/label&gt;</tt></pre>
</div></div>
-<div class="para"><p>Radio buttons, while similar to checkboxes, are controls that specify a set of options in which they are mutually exclusive (user can only pick one):</p></div>
+<div class="paragraph"><p>Radio buttons, while similar to checkboxes, are controls that specify a set of options in which they are mutually exclusive (user can only pick one):</p></div>
<div class="listingblock">
<div class="content">
<pre><tt>&lt;%= radio_button_tag(:age, "child") %&gt;
@@ -444,7 +460,7 @@ output:
<td class="content">Always use labels for each checkbox and radio button. They associate text with a specific option and provide a larger clickable region.</td>
</tr></table>
</div>
-<div class="para"><p>Other form controls we might mention are the text area, password input and hidden input:</p></div>
+<div class="paragraph"><p>Other form controls we might mention are the text area, password input and hidden input:</p></div>
<div class="listingblock">
<div class="content">
<pre><tt>&lt;%= text_area_tag(:message, "Hi, nice site", :size =&gt; "24x6") %&gt;
@@ -457,18 +473,18 @@ output:
&lt;input id="password" name="password" type="password" /&gt;
&lt;input id="parent_id" name="parent_id" type="hidden" value="5" /&gt;</tt></pre>
</div></div>
-<div class="para"><p>Hidden inputs are not shown to the user, but they hold data same as any textual input. Values inside them can be changed with JavaScript.</p></div>
+<div class="paragraph"><p>Hidden inputs are not shown to the user, but they hold data same as any textual input. Values inside them can be changed with JavaScript.</p></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
<img src="./images/icons/tip.png" alt="Tip" />
</td>
-<td class="content">If you're using password input fields (for any purpose), you might want to prevent their values showing up in application logs by activating <tt>filter_parameter_logging(:password)</tt> in your ApplicationController.</td>
+<td class="content">If you&#8217;re using password input fields (for any purpose), you might want to prevent their values showing up in application logs by activating <tt>filter_parameter_logging(:password)</tt> in your ApplicationController.</td>
</tr></table>
</div>
<h3 id="_how_do_forms_with_put_or_delete_methods_work">1.4. How do forms with PUT or DELETE methods work?</h3>
-<div class="para"><p>Rails framework encourages RESTful design of your applications, which means you'll be making a lot of "PUT" and "DELETE" requests (besides "GET" and "POST"). Still, most browsers <em>don't support</em> methods other than "GET" and "POST" when it comes to submitting forms. How does this work, then?</p></div>
-<div class="para"><p>Rails works around this issue by emulating other methods over POST with a hidden input named <tt>"_method"</tt> that is set to reflect the wanted method:</p></div>
+<div class="paragraph"><p>Rails framework encourages RESTful design of your applications, which means you&#8217;ll be making a lot of "PUT" and "DELETE" requests (besides "GET" and "POST"). Still, most browsers <em>don&#8217;t support</em> methods other than "GET" and "POST" when it comes to submitting forms. How does this work, then?</p></div>
+<div class="paragraph"><p>Rails works around this issue by emulating other methods over POST with a hidden input named <tt>"_method"</tt> that is set to reflect the wanted method:</p></div>
<div class="listingblock">
<div class="content">
<pre><tt>form_tag(search_path, :method =&gt; "put")
@@ -482,21 +498,21 @@ output:
&lt;/div&gt;
...</tt></pre>
</div></div>
-<div class="para"><p>When parsing POSTed data, Rails will take into account the special <tt>"_method"</tt> parameter and act as if the HTTP method was the one specified inside it ("PUT" in this example).</p></div>
+<div class="paragraph"><p>When parsing POSTed data, Rails will take into account the special <tt>"_method"</tt> parameter and act as if the HTTP method was the one specified inside it ("PUT" in this example).</p></div>
</div>
<h2 id="_forms_that_deal_with_model_attributes">2. Forms that deal with model attributes</h2>
<div class="sectionbody">
-<div class="para"><p>When we're dealing with an actual model, we will use a different set of form helpers and have Rails take care of some details in the background. In the following examples we will handle an Article model. First, let us have the controller create one:</p></div>
+<div class="paragraph"><p>When we&#8217;re dealing with an actual model, we will use a different set of form helpers and have Rails take care of some details in the background. In the following examples we will handle an Article model. First, let us have the controller create one:</p></div>
<div class="listingblock">
-<div class="title">Example: articles_controller.rb</div>
+<div class="title">articles_controller.rb</div>
<div class="content">
<pre><tt>def new
@article = Article.new
end</tt></pre>
</div></div>
-<div class="para"><p>Now we switch to the view. The first thing to remember is that we should use <tt>form_for</tt> helper instead of <tt>form_tag</tt>, and that we should pass the model name and object as arguments:</p></div>
+<div class="paragraph"><p>Now we switch to the view. The first thing to remember is that we should use <tt>form_for</tt> helper instead of <tt>form_tag</tt>, and that we should pass the model name and object as arguments:</p></div>
<div class="listingblock">
-<div class="title">Example: articles/new.html.erb</div>
+<div class="title">articles/new.html.erb</div>
<div class="content">
<pre><tt>&lt;% form_for :article, @article, :url =&gt; { :action =&gt; "create" } do |f| %&gt;
&lt;%= f.text_field :title %&gt;
@@ -504,7 +520,7 @@ end</tt></pre>
&lt;%= submit_tag "Create" %&gt;
&lt;% end %&gt;</tt></pre>
</div></div>
-<div class="para"><p>There are a few things to note here:</p></div>
+<div class="paragraph"><p>There are a few things to note here:</p></div>
<div class="olist"><ol>
<li>
<p>
@@ -527,7 +543,7 @@ Methods to create form controls are called <strong>on</strong> the form builder
</p>
</li>
</ol></div>
-<div class="para"><p>The resulting HTML is:</p></div>
+<div class="paragraph"><p>The resulting HTML is:</p></div>
<div class="listingblock">
<div class="content">
<pre><tt>&lt;form action="/articles/create" method="post"&gt;
@@ -536,19 +552,19 @@ Methods to create form controls are called <strong>on</strong> the form builder
&lt;input name="commit" type="submit" value="Create" /&gt;
&lt;/form&gt;</tt></pre>
</div></div>
-<div class="para"><p>A nice thing about <tt>f.text_field</tt> and other helper methods is that they will pre-fill the form control with the value read from the corresponding attribute in the model. For example, if we created the article instance by supplying an initial value for the title in the controller:</p></div>
+<div class="paragraph"><p>A nice thing about <tt>f.text_field</tt> and other helper methods is that they will pre-fill the form control with the value read from the corresponding attribute in the model. For example, if we created the article instance by supplying an initial value for the title in the controller:</p></div>
<div class="listingblock">
<div class="content">
<pre><tt>@article = Article.new(:title =&gt; "Rails makes forms easy")</tt></pre>
</div></div>
-<div class="para"><p>&#8230; the corresponding input will be rendered with a value:</p></div>
+<div class="paragraph"><p>... the corresponding input will be rendered with a value:</p></div>
<div class="listingblock">
<div class="content">
<pre><tt>&lt;input id="post_title" name="post[title]" size="30" type="text" value="Rails makes forms easy" /&gt;</tt></pre>
</div></div>
<h3 id="_relying_on_record_identification">2.1. Relying on record identification</h3>
-<div class="para"><p>In the previous chapter we handled the Article model. This model is directly available to users of our application and, following the best practices for developing with Rails, we should declare it <strong>a resource</strong>.</p></div>
-<div class="para"><p>When dealing with RESTful resources, our calls to <tt>form_for</tt> can get significantly easier if we rely on <strong>record identification</strong>. In short, we can just pass the model instance and have Rails figure out model name and the rest:</p></div>
+<div class="paragraph"><p>In the previous chapter we handled the Article model. This model is directly available to users of our application, so&#8201;&#8212;&#8201;following the best practices for developing with Rails&#8201;&#8212;&#8201;we should declare it <strong>a resource</strong>.</p></div>
+<div class="paragraph"><p>When dealing with RESTful resources, our calls to <tt>form_for</tt> can get significantly easier if we rely on <strong>record identification</strong>. In short, we can just pass the model instance and have Rails figure out model name and the rest:</p></div>
<div class="listingblock">
<div class="content">
<pre><tt>## Creating a new article
@@ -563,20 +579,20 @@ form_for(:article, @article, :url =&gt; article_path(@article), :method =&gt; "p
# short-style:
form_for(@article)</tt></pre>
</div></div>
-<div class="para"><p>Notice how the short-style <tt>form_for</tt> invocation is conveniently the same, regardless of the record being new or existing. Record identification is smart enough to figure out if the record is new by asking <tt>record.new_record?</tt>.</p></div>
+<div class="paragraph"><p>Notice how the short-style <tt>form_for</tt> invocation is conveniently the same, regardless of the record being new or existing. Record identification is smart enough to figure out if the record is new by asking <tt>record.new_record?</tt>.</p></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
<img src="./images/icons/warning.png" alt="Warning" />
</td>
-<td class="content">When you're using STI (single-table inheritance) with your models, you can't rely on record identification on a subclass if only their parent class is declared a resource. You will have to specify the model name, <tt>:url</tt> and <tt>:method</tt> explicitly.</td>
+<td class="content">When you&#8217;re using STI (single-table inheritance) with your models, you can&#8217;t rely on record identification on a subclass if only their parent class is declared a resource. You will have to specify the model name, <tt>:url</tt> and <tt>:method</tt> explicitly.</td>
</tr></table>
</div>
</div>
<h2 id="_making_select_boxes_with_ease">3. Making select boxes with ease</h2>
<div class="sectionbody">
-<div class="para"><p>Select boxes in HTML require a significant amount of markup (one <tt>OPTION</tt> element for each option to choose from), therefore it makes the most sense for them to be dynamically generated from data stored in arrays or hashes.</p></div>
-<div class="para"><p>Here is what our wanted markup might look like:</p></div>
+<div class="paragraph"><p>Select boxes in HTML require a significant amount of markup (one <tt>OPTION</tt> element for each option to choose from), therefore it makes the most sense for them to be dynamically generated from data stored in arrays or hashes.</p></div>
+<div class="paragraph"><p>Here is what our wanted markup might look like:</p></div>
<div class="listingblock">
<div class="content">
<pre><tt>&lt;select name="city_id" id="city_id"&gt;
@@ -586,15 +602,14 @@ form_for(@article)</tt></pre>
&lt;option value="12"&gt;Berlin&lt;/option&gt;
&lt;/select&gt;</tt></pre>
</div></div>
-<div class="para"><p>Here we have a list of cities where their names are presented to the user, but internally we want to handle just their IDs so we keep them in value attributes. Let's see how Rails can help out here.</p></div>
+<div class="paragraph"><p>Here we have a list of cities where their names are presented to the user, but internally we want to handle just their IDs so we keep them in value attributes. Let&#8217;s see how Rails can help out here.</p></div>
<h3 id="_the_select_tag_and_options">3.1. The select tag and options</h3>
-<div class="para"><p>The most generic helper is <tt>select_tag</tt>, which &#8212; as the name implies &#8212; simply generates the <tt>SELECT</tt> tag that encapsulates the options:</p></div>
+<div class="paragraph"><p>The most generic helper is <tt>select_tag</tt>, which&#8201;&#8212;&#8201;as the name implies&#8201;&#8212;&#8201;simply generates the <tt>SELECT</tt> tag that encapsulates an options string:</p></div>
<div class="listingblock">
<div class="content">
<pre><tt>&lt;%= select_tag(:city_id, '&lt;option value="1"&gt;Lisabon&lt;/option&gt;...') %&gt;</tt></pre>
</div></div>
-<div class="para"><p>This is a start, but it doesn't dynamically create our option tags. We had to pass them in as a string.</p></div>
-<div class="para"><p>We can generate option tags with the <tt>options_for_select</tt> helper:</p></div>
+<div class="paragraph"><p>This is a start, but it doesn&#8217;t dynamically create our option tags. We can generate option tags with the <tt>options_for_select</tt> helper:</p></div>
<div class="listingblock">
<div class="content">
<pre><tt>&lt;%= options_for_select([['Lisabon', 1], ['Madrid', 2], ...]) %&gt;
@@ -605,13 +620,13 @@ output:
&lt;option value="2"&gt;Madrid&lt;/option&gt;
...</tt></pre>
</div></div>
-<div class="para"><p>For input data we used a nested array where each element has two elements: visible value (name) and internal value (ID).</p></div>
-<div class="para"><p>Now you can combine <tt>select_tag</tt> and <tt>options_for_select</tt> to achieve the desired, complete markup:</p></div>
+<div class="paragraph"><p>For input data we used a nested array where each item has two elements: option text (city name) and option value (city id).</p></div>
+<div class="paragraph"><p>Knowing this, you can combine <tt>select_tag</tt> and <tt>options_for_select</tt> to achieve the desired, complete markup:</p></div>
<div class="listingblock">
<div class="content">
<pre><tt>&lt;%= select_tag(:city_id, options_for_select(...)) %&gt;</tt></pre>
</div></div>
-<div class="para"><p>Sometimes, depending on our application's needs, we also wish a specific option to be pre-selected. The <tt>options_for_select</tt> helper supports this with an optional second argument:</p></div>
+<div class="paragraph"><p>Sometimes, depending on our application&#8217;s needs, we also wish a specific option to be pre-selected. The <tt>options_for_select</tt> helper supports this with an optional second argument:</p></div>
<div class="listingblock">
<div class="content">
<pre><tt>&lt;%= options_for_select(cities_array, 2) %&gt;
@@ -622,14 +637,95 @@ output:
&lt;option value="2" selected="selected"&gt;Madrid&lt;/option&gt;
...</tt></pre>
</div></div>
-<div class="para"><p>So whenever Rails sees that the internal value of an option being generated matches this value, it will add the <tt>selected</tt> attribute to that option.</p></div>
-<h3 id="_select_boxes_for_dealing_with_models">3.2. Select boxes for dealing with models</h3>
-<div class="para"><p>Until now we've covered how to make generic select boxes, but in most cases our form controls will be tied to a specific database model. So, to continue from our previous examples, let's assume that we have a "Person" model with a <tt>city_id</tt> attribute.</p></div>
+<div class="paragraph"><p>So whenever Rails sees that the internal value of an option being generated matches this value, it will add the <tt>selected</tt> attribute to that option.</p></div>
+<h3 id="_select_helpers_for_dealing_with_models">3.2. Select helpers for dealing with models</h3>
+<div class="paragraph"><p>Until now we&#8217;ve covered how to make generic select boxes, but in most cases our form controls will be tied to a specific database model. So, to continue from our previous examples, let&#8217;s assume that we have a "Person" model with a <tt>city_id</tt> attribute.</p></div>
+<div class="paragraph"><p>Consistent with other form helpers, when dealing with models we drop the <tt>"_tag"</tt> suffix from <tt>select_tag</tt> that we used in previous examples:</p></div>
+<div class="listingblock">
+<div class="content">
+<pre><tt># controller:
+@person = Person.new(:city_id =&gt; 2)
+
+# view:
+&lt;%= select(:person, :city_id, [['Lisabon', 1], ['Madrid', 2], ...]) %&gt;</tt></pre>
+</div></div>
+<div class="paragraph"><p>Notice that the third parameter, the options array, is the same kind of argument we pass to <tt>options_for_select</tt>. One thing that we have as an advantage here is that we don&#8217;t have to worry about pre-selecting the correct city if the user already has one&#8201;&#8212;&#8201;Rails will do this for us by reading from <tt>@person.city_id</tt> attribute.</p></div>
+<div class="paragraph"><p>As before, if we were to use <tt>select</tt> helper on a form builder scoped to <tt>@person</tt> object, the syntax would be:</p></div>
+<div class="listingblock">
+<div class="content">
+<pre><tt># select on a form builder
+&lt;%= f.select(:city_id, ...) %&gt;</tt></pre>
+</div></div>
+<h3 id="_option_tags_from_a_collection_of_arbitrary_objects">3.3. Option tags from a collection of arbitrary objects</h3>
+<div class="paragraph"><p>Until now we were generating option tags from nested arrays with the help of <tt>options_for_select</tt> method. Data in our array were raw values:</p></div>
+<div class="listingblock">
+<div class="content">
+<pre><tt>&lt;%= options_for_select([['Lisabon', 1], ['Madrid', 2], ...]) %&gt;</tt></pre>
+</div></div>
+<div class="paragraph"><p>But what if we had a <strong>City</strong> model (perhaps an ActiveRecord one) and we wanted to generate option tags from a collection of those objects? One solution would be to make a nested array by iterating over them:</p></div>
+<div class="listingblock">
+<div class="content">
+<pre><tt>&lt;% cities_array = City.find(:all).map { |city| [city.name, city.id] } %&gt;
+&lt;%= options_for_select(cities_array) %&gt;</tt></pre>
+</div></div>
+<div class="paragraph"><p>This is a perfectly valid solution, but Rails provides us with a less verbose alternative: <tt>options_from_collection_for_select</tt>. This helper expects a collection of arbitrary objects and two additional arguments: the names of the methods to read the option <strong>value</strong> and <strong>text</strong> from, respectively:</p></div>
<div class="listingblock">
<div class="content">
-<pre><tt>...</tt></pre>
+<pre><tt>&lt;%= options_from_collection_for_select(City.all, :id, :name) %&gt;</tt></pre>
+</div></div>
+<div class="paragraph"><p>As the name implies, this only generates option tags. A method to go along with it is <tt>collection_select</tt>:</p></div>
+<div class="listingblock">
+<div class="content">
+<pre><tt>&lt;%= collection_select(:person, :city_id, City.all, :id, :name) %&gt;</tt></pre>
+</div></div>
+<div class="paragraph"><p>To recap, <tt>options_from_collection_for_select</tt> are to <tt>collection_select</tt> what <tt>options_for_select</tt> are to <tt>select</tt>.</p></div>
+<h3 id="_time_zone_and_country_select">3.4. Time zone and country select</h3>
+<div class="paragraph"><p>To leverage time zone support in Rails, we have to ask our users what time zone they are in. Doing so would require generating select options from a list of pre-defined TimeZone objects using <tt>collection_select</tt>, but we can simply use the <tt>time_zone_select</tt> helper that already wraps this:</p></div>
+<div class="listingblock">
+<div class="content">
+<pre><tt>&lt;%= time_zone_select(:person, :city_id) %&gt;</tt></pre>
+</div></div>
+<div class="paragraph"><p>There is also <tt>time_zone_options_for_select</tt> helper for a more manual (therefore more customizable) way of doing this. Read the API documentation to learn about the possible arguments for these two methods.</p></div>
+<div class="paragraph"><p>When it comes to country select, Rails <em>used</em> to have the built-in helper <tt>country_select</tt> but was extracted to a plugin.
+TODO: plugin URL</p></div>
+</div>
+<h2 id="_miscellaneous">4. Miscellaneous</h2>
+<div class="sectionbody">
+<h3 id="_file_upload_form">4.1. File upload form</h3>
+<div class="paragraph"><p>:multipart - If set to true, the enctype is set to "multipart/form-data".</p></div>
+<h3 id="_scoping_out_form_controls_with_tt_fields_for_tt">4.2. Scoping out form controls with <tt>fields_for</tt></h3>
+<div class="paragraph"><p>Creates a scope around a specific model object like <tt>form_for</tt>, but doesn’t create the form tags themselves. This makes <tt>fields_for</tt> suitable for specifying additional model objects in the same form:</p></div>
+<h3 id="_making_custom_form_builders">4.3. Making custom form builders</h3>
+<div class="paragraph"><p>You can also build forms using a customized FormBuilder class. Subclass FormBuilder and override or define some more helpers, then use your custom builder. For example, let’s say you made a helper to automatically add labels to form inputs.</p></div>
+<div class="listingblock">
+<div class="content">
+<pre><tt>&lt;% form_for :person, @person, :url =&gt; { :action =&gt; "update" }, :builder =&gt; LabellingFormBuilder do |f| %&gt;
+ &lt;%= f.text_field :first_name %&gt;
+ &lt;%= f.text_field :last_name %&gt;
+ &lt;%= text_area :person, :biography %&gt;
+ &lt;%= check_box_tag "person[admin]", @person.company.admin? %&gt;
+&lt;% end %&gt;</tt></pre>
+</div></div>
+<div class="ulist"><ul>
+<li>
+<p>
+<tt>form_for</tt> within a namespace
+</p>
+</li>
+</ul></div>
+<div class="listingblock">
+<div class="content">
+<pre><tt>select_tag(name, option_tags = nil, html_options = { :multiple, :disabled })
+
+select(object, method, choices, options = {}, html_options = {})
+options_for_select(container, selected = nil)
+
+collection_select(object, method, collection, value_method, text_method, options = {}, html_options = {})
+options_from_collection_for_select(collection, value_method, text_method, selected = nil)
+
+time_zone_options_for_select(selected = nil, priority_zones = nil, model = ::ActiveSupport::TimeZone)
+time_zone_select(object, method, priority_zones = nil, options = {}, html_options = {})</tt></pre>
</div></div>
-<div class="para"><p>&#8230;</p></div>
</div>
</div>