0
@@ -73,30 +73,54 @@ module ActionView
0
# There are also methods for helping to build form tags in link:classes/ActionView/Helpers/FormOptionsHelper.html,
0
# link:classes/ActionView/Helpers/DateHelper.html, and link:classes/ActionView/Helpers/ActiveRecordHelper.html
0
- # Creates a form and a scope around a specific model object that is used as a base for questioning about
0
- # values for the fields.
0
+ # Creates a form and a scope around a specific model object that is used as
0
+ # a base for questioning about values for the fields.
0
- # <% form_for :person, @person, :url => { :action => "update" } do |f| %>
0
- # <%= f.error_messages %>
0
- # First name: <%= f.text_field :first_name %>
0
- # Last name : <%= f.text_field :last_name %>
0
- # Biography : <%= f.text_area :biography %>
0
- # Admin? : <%= f.check_box :admin %>
0
+ # Rails provides succint resource-oriented form generation with +form_for+
0
+ # <% form_for @offer do |f| %>
0
+ # <%= f.label :version, 'Version' %>:
0
+ # <%= f.text_field :version %><br />
0
+ # <%= f.label :author, 'Author' %>:
0
+ # <%= f.text_field :author %><br />
0
- # Worth noting is that the form_for tag is called in a ERb evaluation block, not an ERb output block. So that's <tt><% %></tt>,
0
- # not <tt><%= %></tt>. Also worth noting is that form_for yields a <tt>form_builder</tt> object, in this example as <tt>f</tt>, which emulates
0
- # the API for the stand-alone FormHelper methods, but without the object name. So instead of <tt>text_field :person, :name</tt>,
0
- # you get away with <tt>f.text_field :name</tt>. Notice that you can even do <tt><%= f.error_messages %></tt> to display the
0
- # error messsages of the model object in question.
0
+ # There, +form_for+ is able to generate the rest of RESTful parameters
0
+ # based on introspection on the record, but to understand what it does we
0
+ # need to dig first into the alternative generic usage it is based upon.
0
+ # === Generic form_for
0
- # Even further, the form_for method allows you to more easily escape the instance variable convention. So while the stand-alone
0
- # approach would require <tt>text_field :person, :name, :object => person</tt>
0
- # to work with local variables instead of instance ones, the form_for calls remain the same. You simply declare once with
0
- # <tt>:person, person</tt> and all subsequent field calls save <tt>:person</tt> and <tt>:object => person</tt>.
0
+ # The generic way to call +form_for+ requires a few arguments:
0
- # Also note that form_for doesn't create an exclusive scope. It's still possible to use both the stand-alone FormHelper methods
0
- # and methods from FormTagHelper. For example:
0
+ # <% form_for :person, @person, :url => { :action => "update" } do |f| %>
0
+ # <%= f.error_messages %>
0
+ # First name: <%= f.text_field :first_name %><br />
0
+ # Last name : <%= f.text_field :last_name %><br />
0
+ # Biography : <%= f.text_area :biography %><br />
0
+ # Admin? : <%= f.check_box :admin %><br />
0
+ # Worth noting is that the +form_for+ tag is called in a ERb evaluation block,
0
+ # not an ERb output block. So that's <tt><% %></tt>, not <tt><%= %></tt>. Also
0
+ # worth noting is that +form_for+ yields a form builder object, in this
0
+ # example as +f+, which emulates the API for the stand-alone FormHelper
0
+ # methods, but without the object name. So instead of <tt>text_field :person, :name</tt>,
0
+ # you get away with <tt>f.text_field :name</tt>. Notice that you can even do
0
+ # <tt><%= f.error_messages %></tt> to display the error messsages of the model
0
+ # Even further, the +form_for+ method allows you to more easily escape the
0
+ # instance variable convention. So while the stand-alone approach would require
0
+ # <tt>text_field :person, :name, :object => person</tt> to work with local
0
+ # variables instead of instance ones, the +form_for+ calls remain the same.
0
+ # You simply declare once with <tt>:person, person</tt> and all subsequent
0
+ # field calls save <tt>:person</tt> and <tt>:object => person</tt>.
0
+ # Also note that +form_for+ doesn't create an exclusive scope. It's still
0
+ # possible to use both the stand-alone FormHelper methods and methods from
0
+ # FormTagHelper. For example:
0
# <% form_for :person, @person, :url => { :action => "update" } do |f| %>
0
# First name: <%= f.text_field :first_name %>
0
@@ -105,22 +129,26 @@ module ActionView
0
# Admin? : <%= check_box_tag "person[admin]", @person.company.admin? %>
0
- # Note: This also works for the methods in FormOptionHelper and DateHelper that are designed to work with an object as base,
0
- # like FormOptionHelper#collection_select and DateHelper#datetime_select.
0
+ # This also works for the methods in FormOptionHelper and DateHelper that are
0
+ # designed to work with an object as base, like FormOptionHelper#collection_select
0
+ # and DateHelper#datetime_select.
0
- # HTML attributes for the form tag can be given as <tt>:html => {...}</tt>. For example:
0
+ # HTML attributes for the form tag can be given as <tt>:html => {...}</tt>.
0
# <% form_for :person, @person, :html => {:id => 'person_form'} do |f| %>
0
- # The above form will then have the <tt>id</tt> attribute with the value </tt>person_form</tt>, which you can then
0
- # style with CSS or manipulate with JavaScript.
0
+ # The above form will then have the +id+ attribute with the value "person_form",
0
+ # which you can then style with CSS or manipulate with JavaScript.
0
# === Relying on record identification
0
- # In addition to manually configuring the form_for call, you can also rely on record identification, which will use
0
- # the conventions and named routes of that approach. Examples:
0
+ # As we said above, in addition to manually configuring the +form_for+ call,
0
+ # you can rely on record identification, which will use the conventions and
0
+ # named routes of that approach. This is the preferred way to use +form_for+
0
# <% form_for(@post) do |f| %>
0
@@ -140,7 +168,7 @@ module ActionView
0
# This will expand to be the same as:
0
- # <% form_for :post, @post, :url => posts_path, :html => { :class => "new_post", :id => "new_post" } do |f| %>
0
+ # <% form_for :post, Post.new, :url => posts_path, :html => { :class => "new_post", :id => "new_post" } do |f| %>
0
@@ -150,7 +178,7 @@ module ActionView
0
- # And for namespaced routes, like admin_post_url:
0
+ # And for namespaced routes, like +admin_post_url+:
0
# <% form_for([:admin, @post]) do |f| %>