From 9111f4268b1700bc061dcf82436484089dd8a9ba Mon Sep 17 00:00:00 2001 From: Xavier Noria Date: Sun, 11 Apr 2010 02:49:57 -0700 Subject: second pass to the intro rdoc of form_helper.rb --- actionpack/lib/action_view/helpers/form_helper.rb | 68 ++++++++++++++++++----- 1 file changed, 55 insertions(+), 13 deletions(-) diff --git a/actionpack/lib/action_view/helpers/form_helper.rb b/actionpack/lib/action_view/helpers/form_helper.rb index 192b365bee..c1b1261ada 100644 --- a/actionpack/lib/action_view/helpers/form_helper.rb +++ b/actionpack/lib/action_view/helpers/form_helper.rb @@ -11,21 +11,25 @@ module ActionView # Form helpers are designed to make working with resources much easier # compared to using vanilla HTML. # - # Model-based forms are created with +form_for+. That method yields a form + # Forms for models are created with +form_for+. That method yields a form # builder that knows the model the form is about. The form builder is thus # able to generate default values for input fields that correspond to model - # attributes, and also convenient element names, IDs, endpoints, etc. + # attributes, and also convenient names, IDs, endpoints, etc. # # Conventions in the generated field names allow controllers to receive form # data nicely structured in +params+ with no effort on your side. # - # For example, to create a new +Person+ resource you typically set up a new - # instance in PeopleController#new action, @person, and - # write the form in new.html.erb this way: + # For example, to create a new person you typically set up a new instance of + # +Person+ in the PeopleController#new action, @person, and + # pass it to +form_for+: # # <%= form_for @person do |f| %> - # <%= f.text_field :first_name %> - # <%= f.text_field :last_name %> + # <%= f.label :first_name %>: + # <%= f.text_field :first_name %>
+ # + # <%= f.label :last_name %>: + # <%= f.text_field :last_name %>
+ # # <%= f.submit %> # <% end %> # @@ -35,16 +39,54 @@ module ActionView #
# #
- # - # + # : + #
+ # + # : + #
+ # # # # - # Because of the names of the input fields, the controller gets a :person - # nested hash in +params+ with the corresponding first and last names. That hash - # is ready to be passed to Person.create like this: + # As you see, the HTML reflects knowledge about the resource in several spots, + # like the path the form should be submitted to, or the names of the input fields. + # + # In particular, thanks to the conventions followed in the generated field names, the + # controller gets a nested hash params[:person] with the person attributes + # set in the form. That hash is ready to be passed to Person.create: + # + # if @person = Person.create(params[:person]) + # # success + # else + # # error handling + # end + # + # Interestingly, the exact same view code in the previous example can be used to edit + # a person. If @person is an existing record with name "John Smith" and ID 256, + # the code above as is would yield instead: + # + #
+ #
+ # + # + #
+ # : + #
+ # + # : + #
+ # + # + #
+ # + # Note that the endpoint, default values, and submit button label are tailored for @person. + # That works that way because the involved helpers know whether the resource is a new record or not, + # and generate HTML accordingly. + # + # The controller would receive the form data again in params[:person], ready to be + # passed to Person#update_attributes: # - # if person = Person.create(params[:person]) + # if @person.update_attributes(params[:person]) # # success # else # # error handling -- cgit v1.2.3 From fb4b454de1754dbb3bf97974191f2d8860dbba57 Mon Sep 17 00:00:00 2001 From: Xavier Noria Date: Sun, 11 Apr 2010 02:56:19 -0700 Subject: your beloved typo only spotted in github diff no matter how careful you were before pushing --- actionpack/lib/action_view/helpers/form_helper.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/actionpack/lib/action_view/helpers/form_helper.rb b/actionpack/lib/action_view/helpers/form_helper.rb index c1b1261ada..80a00b2a10 100644 --- a/actionpack/lib/action_view/helpers/form_helper.rb +++ b/actionpack/lib/action_view/helpers/form_helper.rb @@ -65,7 +65,7 @@ module ActionView # a person. If @person is an existing record with name "John Smith" and ID 256, # the code above as is would yield instead: # - #
+ # #
# # -- cgit v1.2.3