aboutsummaryrefslogtreecommitdiffstats
path: root/railties/guides/source/form_helpers.textile
diff options
context:
space:
mode:
Diffstat (limited to 'railties/guides/source/form_helpers.textile')
-rw-r--r--railties/guides/source/form_helpers.textile26
1 files changed, 14 insertions, 12 deletions
diff --git a/railties/guides/source/form_helpers.textile b/railties/guides/source/form_helpers.textile
index fe0f8f1ac9..050486a5a4 100644
--- a/railties/guides/source/form_helpers.textile
+++ b/railties/guides/source/form_helpers.textile
@@ -21,7 +21,7 @@ h3. Dealing with Basic Forms
The most basic form helper is +form_tag+.
<erb>
-<% form_tag do %>
+<%= form_tag do %>
Form contents
<% end %>
</erb>
@@ -59,14 +59,14 @@ To create this form you will use +form_tag+, +label_tag+, +text_field_tag+, and
A basic search form
<html>
-<% form_tag(search_path, :method => "get") do %>
+<%= form_tag(search_path, :method => "get") do %>
<%= label_tag(:q, "Search for:") %>
<%= text_field_tag(:q) %>
<%= submit_tag("Search") %>
<% end %>
</html>
-TIP: +search_path+ can be a named route specified in "routes.rb": <pre>map.search "search", :controller => "search"</pre>
+TIP: +search_path+ can be a named route specified in "routes.rb": <br /><tt>map.search "search", :controller => "search"</tt>
The above view code will result in the following markup:
@@ -222,7 +222,7 @@ end
The corresponding view +app/views/articles/new.html.erb+ using +form_for+ looks like this:
<erb>
-<% form_for :article, @article, :url => { :action => "create" }, :html => {:class => "nifty_form"} do |f| %>
+<%= form_for :article, @article, :url => { :action => "create" }, :html => {:class => "nifty_form"} do |f| %>
<%= f.text_field :title %>
<%= f.text_area :body, :size => "60x12" %>
<%= submit_tag "Create" %>
@@ -253,7 +253,7 @@ The helper methods called on the form builder are identical to the model object
You can create a similar binding without actually creating +&lt;form&gt;+ tags with the +fields_for+ helper. This is useful for editing additional model objects with the same form. For example if you had a Person model with an associated ContactDetail model you could create a form for creating both like so:
<erb>
-<% form_for :person, @person, :url => { :action => "create" } do |person_form| %>
+<%= form_for :person, @person, :url => { :action => "create" } do |person_form| %>
<%= person_form.text_field :name %>
<% fields_for @person.contact_detail do |contact_details_form| %>
<%= contact_details_form.text_field :phone_number %>
@@ -429,7 +429,7 @@ As with other helpers, if you were to use the +select+ helper on a form builder
<%= f.select(:city_id, ...) %>
</erb>
-WARNING: If you are using +select+ (or similar helpers such as +collection_select+, +select_tag+) to set a +belongs_to+ association you must pass the name of the foreign key (in the example above +city_id+), not the name of association itself. If you specify +city+ instead of +city_id+ Active Record will raise an error along the lines of <pre> ActiveRecord::AssociationTypeMismatch: City(#17815740) expected, got String(#1138750) </pre> when you pass the +params+ hash to +Person.new+ or +update_attributes+. Another way of looking at this is that form helpers only edit attributes. You should also be aware of the potential security ramifications of allowing users to edit foreign keys directly. You may wish to consider the use of +attr_protected+ and +attr_accessible+. For further details on this, see the "Ruby On Rails Security Guide":security.html#_mass_assignment.
+WARNING: If you are using +select+ (or similar helpers such as +collection_select+, +select_tag+) to set a +belongs_to+ association you must pass the name of the foreign key (in the example above +city_id+), not the name of association itself. If you specify +city+ instead of +city_id+ Active Record will raise an error along the lines of <tt> ActiveRecord::AssociationTypeMismatch: City(#17815740) expected, got String(#1138750) </tt> when you pass the +params+ hash to +Person.new+ or +update_attributes+. Another way of looking at this is that form helpers only edit attributes. You should also be aware of the potential security ramifications of allowing users to edit foreign keys directly. You may wish to consider the use of +attr_protected+ and +attr_accessible+. For further details on this, see the "Ruby On Rails Security Guide":security.html#_mass_assignment.
h4. Option Tags from a Collection of Arbitrary Objects
@@ -554,11 +554,11 @@ A common task is uploading some sort of file, whether it's a picture of a person
The following two forms both upload a file.
<erb>
-<% form_tag({:action => :upload}, :multipart => true) do %>
+<%= form_tag({:action => :upload}, :multipart => true) do %>
<%= file_field_tag 'picture' %>
<% end %>
-<% form_for @person, :html => {:multipart => true} do |f| %>
+<%= form_for @person, :html => {:multipart => true} do |f| %>
<%= f.file_field :picture %>
<% end %>
</erb>
@@ -591,7 +591,7 @@ h3. Customizing Form Builders
As mentioned previously the object yielded by +form_for+ and +fields_for+ is an instance of FormBuilder (or a subclass thereof). Form builders encapsulate the notion of displaying form elements for a single object. While you can of course write helpers for your forms in the usual way you can also subclass FormBuilder and add the helpers there. For example
<erb>
-<% form_for @person do |f| %>
+<%= form_for @person do |f| %>
<%= text_field_with_label f, :first_name %>
<% end %>
</erb>
@@ -599,7 +599,7 @@ As mentioned previously the object yielded by +form_for+ and +fields_for+ is an
can be replaced with
<erb>
-<% form_for @person, :builder => LabellingFormBuilder do |f| %>
+<%= form_for @person, :builder => LabellingFormBuilder do |f| %>
<%= f.text_field :first_name %>
<% end %>
</erb>
@@ -631,7 +631,7 @@ action for a Person model, +params[:model]+ would usually be a hash of all the a
Fundamentally HTML forms don't know about any sort of structured data, all they generate is name–value pairs, where pairs are just plain strings. The arrays and hashes you see in your application are the result of some parameter naming conventions that Rails uses.
-TIP: You may find you can try out examples in this section faster by using the console to directly invoke Rails' parameter parser. For example <pre> ActionController::UrlEncodedPairParser.parse_query_parameters "name=fred&phone=0123456789" # => {"name"=>"fred", "phone"=>"0123456789"} </pre>
+TIP: You may find you can try out examples in this section faster by using the console to directly invoke Rails' parameter parser. For example <tt> ActionController::UrlEncodedPairParser.parse_query_parameters "name=fred&phone=0123456789" # => {"name"=>"fred", "phone"=>"0123456789"} </tt>
h4. Basic Structures
@@ -694,7 +694,7 @@ The previous sections did not use the Rails form helpers at all. While you can c
You might want to render a form with a set of edit fields for each of a person's addresses. For example:
<erb>
-<% form_for @person do |person_form| %>
+<%= form_for @person do |person_form| %>
<%= person_form.text_field :name %>
<% for address in @person.addresses %>
<% person_form.fields_for address, :index => address do |address_form|%>
@@ -762,6 +762,8 @@ h3. Changelog
"Lighthouse ticket":http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/1
+* April 6, 2010: Fixed document to validate XHTML 1.0 Strict. "Jaime Iniesta":http://jaimeiniesta.com
+
h3. Authors
* Mislav Marohnić <mislav.marohnic@gmail.com>