public
Description: PLEASE CHECK http://github.com/lifo/docrails/wikis
Homepage: http://weblog.rubyonrails.org/2008/5/2/help-improve-rails-documentation-on-git-branch
Clone URL: git://github.com/lifo/docrails.git
revised docs of form_for

This revision encourages the modern resource-oriented form_for usage. In 
addition corrects some markup and other details.
fxn (author)
Fri May 16 14:44:51 -0700 2008
commit  0fc3381aa5359f31b36057d7bfba2e0eb6a3c064
tree    ccaef6bd8b2d81548029e1036f26a7286ab01d91
parent  70e4bcf5cb039bea073851faed2df9f465e6f2f8
...
73
74
75
76
77
 
 
78
79
80
81
82
83
84
 
 
 
 
 
 
 
 
85
86
87
88
89
90
91
 
 
 
 
 
92
93
94
95
96
 
97
98
99
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
100
101
102
...
105
106
107
108
109
 
 
 
110
111
 
 
112
113
114
115
116
117
118
 
 
119
120
121
122
123
 
 
 
 
124
125
126
...
140
141
142
143
 
144
145
146
...
150
151
152
153
 
154
155
156
...
73
74
75
 
 
76
77
78
 
 
 
 
 
 
79
80
81
82
83
84
85
86
87
88
 
 
 
 
 
89
90
91
92
93
94
 
 
 
 
95
96
 
 
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
...
129
130
131
 
 
132
133
134
135
 
136
137
138
139
140
141
142
 
 
143
144
145
146
147
 
 
148
149
150
151
152
153
154
...
168
169
170
 
171
172
173
174
...
178
179
180
 
181
182
183
184
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
     module FormHelper
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
       #
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
+ # like this:
0
+ #
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
       # <% end %>
0
       #
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
+ #
0
+ # === Generic form_for
0
       #
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
       #
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
+ # <% end %>
0
+ #
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
+ # object in question.
0
+ #
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
+ #
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
       #
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
       # <% end %>
0
       #
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
       #
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
+ # For example:
0
       #
0
       # <% form_for :person, @person, :html => {:id => 'person_form'} do |f| %>
0
       # ...
0
       # <% end %>
0
       #
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
       #
0
       # === Relying on record identification
0
       #
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
+ # nowadays:
0
       #
0
       # <% form_for(@post) do |f| %>
0
       # ...
0
@@ -140,7 +168,7 @@ module ActionView
0
       #
0
       # This will expand to be the same as:
0
       #
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
       # ...
0
       # <% end %>
0
       #
0
@@ -150,7 +178,7 @@ module ActionView
0
       # ...
0
       # <% end %>
0
       #
0
- # And for namespaced routes, like admin_post_url:
0
+ # And for namespaced routes, like +admin_post_url+:
0
       #
0
       # <% form_for([:admin, @post]) do |f| %>
0
       # ...

Comments

    No one has commented yet.