Generating Forms

HTML provides a number of elements, attributes, and attribute values that control how input is gathered. You certainly could hand-code your form directly into the template, but there really is no need to do that.

In this section, we will cover a number of helpers that Rails provides that assist with this process. In Using Helpers, we’ll show you how you can create your own helpers.

HTML provides a number of ways to collect data in forms. A few of the more common means are shown in the following screenshot. Note that the form itself is not representative of any sort of typical use; in general, you’ll use only a subset of these methods to collect data.

images/form_helpers.png

Let’s look at the template that was used to produce that form:

1: <%=​ form_for(​:model​) ​do​ |form| ​%>
<p>
<%=​ form.​label​ ​:input​ ​%>
<%=​ form.​text_field​ ​:input​, ​:placeholder​ => ​'Enter text here...'​ ​%>
5: </p>
<p>
<%=​ form.​label​ ​:address​, ​:style​ => ​'float: left'​ ​%>
<%=​ form.​text_area​ ​:address​, ​:rows​ => 3, ​:cols​ => 40 ​%>
10: </p>
<p>
<%=​ form.​label​ ​:color​ ​%>​:
<%=​ form.​radio_button​ ​:color​, ​'red'​ ​%>
15: <%=​ form.​label​ ​:red​ ​%>
<%=​ form.​radio_button​ ​:color​, ​'yellow'​ ​%>
<%=​ form.​label​ ​:yellow​ ​%>
<%=​ form.​radio_button​ ​:color​, ​'green'​ ​%>
<%=​ form.​label​ ​:green​ ​%>
20: </p>
<p>
<%=​ form.​label​ ​'condiment'​ ​%>​:
<%=​ form.​check_box​ ​:ketchup​ ​%>
25: <%=​ form.​label​ ​:ketchup​ ​%>
<%=​ form.​check_box​ ​:mustard​ ​%>
<%=​ form.​label​ ​:mustard​ ​%>
<%=​ form.​check_box​ ​:mayonnaise​ ​%>
<%=​ form.​label​ ​:mayonnaise​ ​%>
30: </p>
<p>
<%=​ form.​label​ ​:priority​ ​%>​:
<%=​ form.​select​ ​:priority​, (1..10) ​%>
35: </p>
<p>
<%=​ form.​label​ ​:start​ ​%>​:
<%=​ form.​date_select​ ​:start​ ​%>
40: </p>
<p>
<%=​ form.​label​ ​:alarm​ ​%>​:
<%=​ form.​time_select​ ​:alarm​ ​%>
45: </p>
<%​ ​end​ ​%>

In that template, you will see a number of labels, such as the one on line 3. You use labels to associate text with an input field for a specified attribute. The text of the label will default to the attribute name unless you specify it explicitly.

You use the text_field and text_area helpers (on lines 4 and 9, respectively) to gather single-line and multiline input fields. You may specify a placeholder, which will be displayed inside the field until the user provides a value. Not every browser supports this function, but those that don’t simply will display an empty box. Since this will degrade gracefully, there is no need for you to design to the least common denominator—make use of this feature, because those who can see it will benefit from it immediately.

Placeholders are one of the many small “fit and finish” features provided with HTML5, and once again, Rails is ready even if the browser your users have installed is not. You can use the search_field, telephone_field, url_field, email_field, number_field, and range_field helpers to prompt for a specific type of input. How the browser will make use of this information varies. Some may display the field slightly differently to more clearly identify its function. Safari on Mac, for example, will display search fields with rounded corners and will insert a little x for clearing the field once data entry begins. Some may provide added validation. For example, Opera will validate URL fields prior to submission. The iPad will even adjust the virtual onscreen keyboard to provide ready access to characters such as the @ sign when entering an email address.

Although the support for these functions varies by browser, those that don’t provide extra support for these functions simply display a plain, unadorned input box. Once again, nothing is gained by waiting. If you have an input field that’s expected to contain an email address, don’t simply use text_field—go ahead and start using email_field now.

Lines 14, 24, and 34 demonstrate three different ways to provide a constrained set of options. Although the display may vary a bit from browser to browser, these approaches are all well supported across all browsers. The select method is particularly flexible—it can be passed an Enumeration as shown here, an array of pairs of name-value pairs, or a Hash. A number of form options helpers[103] are available to produce such lists from various sources, including the database.

Finally, lines 39 and 44 show prompts for a date and time, respectively. As you might expect by now, Rails provides plenty of options here too.[104]

Not shown in this example are hidden_field and password_field. A hidden field is not displayed at all, but the value is passed back to the server. This may be useful as an alternative to storing transient data in sessions, enabling data from one request to be passed onto the next. Password fields are displayed, but the text entered in them is obscured.

This is more than an adequate starter set for most needs. Should you find that you have additional needs, you’re quite likely to find a helper or gem is already available for you. A good place to start is with the Rails Guides.[105]

Meanwhile, let’s explore how the data form’s submit is processed.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.27.232