From 95d13cca7ba0433d9a95a20c3f28904ab6c76249 Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Thu, 8 Apr 2010 13:34:40 -0300 Subject: form_for :as rubydoc and tidy up a bit the form_for doc --- actionpack/CHANGELOG | 2 + actionpack/lib/action_view/helpers/form_helper.rb | 51 +++++++++-------------- 2 files changed, 21 insertions(+), 32 deletions(-) (limited to 'actionpack') diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG index 708683747b..33084d539f 100644 --- a/actionpack/CHANGELOG +++ b/actionpack/CHANGELOG @@ -1,5 +1,7 @@ *Rails 3.0.0 [beta 3] (pending)* +* New option :as added to form_for allows to change the object name. The old <% form_for :client, @post %> becomes <% form_for @post, :as => :client %> [spastorino] + * Removed verify method in controllers. [JV] It's now available as a plugin at http://github.com/rails/verification diff --git a/actionpack/lib/action_view/helpers/form_helper.rb b/actionpack/lib/action_view/helpers/form_helper.rb index a3453cc47a..d2b3e2b214 100644 --- a/actionpack/lib/action_view/helpers/form_helper.rb +++ b/actionpack/lib/action_view/helpers/form_helper.rb @@ -45,8 +45,8 @@ module ActionView # # If you are using a partial for your form fields, you can use this shortcut: # - # <%= form_for :person, @person do |form| %> - # <%= render :partial => f %> + # <%= form_for @person do |form| %> + # <%= render form %> # <%= submit_tag 'Create' %> # <% end %> # @@ -129,9 +129,8 @@ module ActionView # Admin? : <%= f.check_box :admin %>
# <% end %> # - # There, the first argument is a symbol or string with the name of the - # object the form is about, and also the name of the instance variable - # the object is stored in. + # There, the argument is a symbol or string with the name of the + # object the form is about. # # The form builder acts as a regular form helper that somehow carries the # model. Thus, the idea is that @@ -142,26 +141,7 @@ module ActionView # # <%= text_field :person, :first_name %> # - # If the instance variable is not @person you can pass the actual - # record as the second argument: - # - # <%= form_for :person, person do |f| %> - # ... - # <% end %> - # - # In that case you can think - # - # <%= f.text_field :first_name %> - # - # gets expanded to - # - # <%= text_field :person, :first_name, :object => person %> - # - # You can even display error messages of the wrapped model this way: - # - # <%= f.error_messages %> - # - # In any of its variants, the rightmost argument to +form_for+ is an + # The rightmost argument to +form_for+ is an # optional hash of options: # # * :url - The URL the form is submitted to. It takes the same @@ -177,7 +157,7 @@ module ActionView # possible to use both the stand-alone FormHelper methods and methods # from FormTagHelper. For example: # - # <%= form_for :person, @person do |f| %> + # <%= form_for @person do |f| %> # First name: <%= f.text_field :first_name %> # Last name : <%= f.text_field :last_name %> # Biography : <%= text_area :person, :biography %> @@ -203,7 +183,7 @@ module ActionView # # is equivalent to something like: # - # <%= form_for :post, @post, :url => post_path(@post), :html => { :method => :put, :class => "edit_post", :id => "edit_post_45" } do |f| %> + # <%= form_for @post, :as => :post, :url => post_path(@post), :html => { :method => :put, :class => "edit_post", :id => "edit_post_45" } do |f| %> # ... # <% end %> # @@ -213,9 +193,9 @@ module ActionView # ... # <% end %> # - # expands to + # is equivalent to something like: # - # <%= form_for :post, Post.new, :url => posts_path, :html => { :class => "new_post", :id => "new_post" } do |f| %> + # <%= form_for @post, :as => :post, :url => post_path(@post), :html => { :class => "new_post", :id => "new_post" } do |f| %> # ... # <% end %> # @@ -225,6 +205,13 @@ module ActionView # ... # <% end %> # + # If you have an object that needs to be represented as a different + # parameter, like a Client that acts as a Person: + # + # <%= form_for(@post, :as => :client do |f| %> + # ... + # <% end %> + # # And for namespaced routes, like +admin_post_url+: # # <%= form_for([:admin, @post]) do |f| %> @@ -245,13 +232,13 @@ module ActionView # # Example: # - # <%= form_for(:post, @post, :remote => true, :html => { :id => 'create-post', :method => :put }) do |f| %> + # <%= form_for(@post, :remote => true) do |f| %> # ... # <% end %> # # The HTML generated for this would be: # - #
+ # #
# #
@@ -265,7 +252,7 @@ module ActionView # custom builder. For example, let's say you made a helper to # automatically add labels to form inputs. # - # <%= form_for :person, @person, :url => { :action => "create" }, :builder => LabellingFormBuilder do |f| %> + # <%= form_for @person, :url => { :action => "create" }, :builder => LabellingFormBuilder do |f| %> # <%= f.text_field :first_name %> # <%= f.text_field :last_name %> # <%= text_area :person, :biography %> -- cgit v1.2.3 From ef2ec071deba316d2f1f756f9591a153eb951219 Mon Sep 17 00:00:00 2001 From: Xavier Noria Date: Sat, 10 Apr 2010 16:49:39 -0700 Subject: makes a pass to the intro rdoc of form_helper.rb, will revise form_for tomorrow --- actionpack/lib/action_view/helpers/form_helper.rb | 112 ++++++++-------------- 1 file changed, 38 insertions(+), 74 deletions(-) (limited to 'actionpack') diff --git a/actionpack/lib/action_view/helpers/form_helper.rb b/actionpack/lib/action_view/helpers/form_helper.rb index d2b3e2b214..92ec6ee632 100644 --- a/actionpack/lib/action_view/helpers/form_helper.rb +++ b/actionpack/lib/action_view/helpers/form_helper.rb @@ -8,90 +8,54 @@ require 'active_support/core_ext/object/blank' module ActionView module Helpers - # Form helpers are designed to make working with models much easier - # compared to using just standard HTML elements by providing a set of - # methods for creating forms based on your models. This helper generates - # the HTML for forms, providing a method for each sort of input - # (e.g., text, password, select, and so on). When the form is submitted - # (i.e., when the user hits the submit button or form.submit is - # called via JavaScript), the form inputs will be bundled into the - # params object and passed back to the controller. + # Form helpers are designed to make working with resources much easier + # compared to using vanilla HTML. # - # There are two types of form helpers: those that specifically work with - # model attributes and those that don't. This helper deals with those that - # work with model attributes; to see an example of form helpers that don't - # work with model attributes, check the ActionView::Helpers::FormTagHelper - # documentation. + # There are methods to generate all kinds of input fields and the form + # element itself. They get convenient names, IDs, endpoints, etc. so that + # you can work at the model level. Thanks to conventions in the HTML they + # generate controllers receive form data nicely structured in +params+. # - # The core method of this helper, form_for, gives you the ability to create - # a form for a model instance; for example, let's say that you have a model - # Person and want to create a new instance of it: + # Model-based forms 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. # - # # Note: a @person variable will have been created in the controller. - # # For example: @person = Person.new - # <%= form_for @person do |f| %> - # <%= f.text_field :first_name %> - # <%= f.text_field :last_name %> - # <%= submit_tag 'Create' %> - # <% end %> + # Conventions in the generated field names allow controllers to receive form + # data nicely structured in +params+ with no effort on your side. # - # The HTML generated for this would be: + # 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: # - # - # - # - # - # + # <%= form_for @person do |f| %> + # <%= f.text_field :first_name %> + # <%= f.text_field :last_name %> + # <%= f.submit %> + # <% end %> # - # If you are using a partial for your form fields, you can use this shortcut: + # The HTML generated for this would be (modulus formatting): # - # <%= form_for @person do |form| %> - # <%= render form %> - # <%= submit_tag 'Create' %> - # <% end %> + #
+ #
+ # + #
+ # + # + # + #
# - # This example will render the people/_form partial, setting a - # local variable called form which references the yielded - # FormBuilder. The params object created when this form is - # submitted would look like: + # 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: # - # {"action"=>"create", "controller"=>"persons", "person"=>{"first_name"=>"William", "last_name"=>"Smith"}} + # if person = Person.create(params[:person]) + # # success + # else + # # error handling + # end # - # The params hash has a nested person value, which can therefore - # be accessed with params[:person] in the controller. If were - # editing/updating an instance (e.g., Person.find(1) rather than - # Person.new in the controller), the objects attribute values are - # filled into the form (e.g., the person_first_name field would - # have that person's first name in it). - # - # If the object name contains square brackets the id for the object will be - # inserted. For example: - # - # <%= text_field "person[]", "name" %> - # - # ...will generate the following ERb. - # - # - # - # If the helper is being used to generate a repetitive sequence of similar - # form elements, for example in a partial used by - # render_collection_of_partials, the index option may - # come in handy. Example: - # - # <%= text_field "person", "name", "index" => 1 %> - # - # ...becomes... - # - # - # - # An index option may also be passed to form_for and - # fields_for. This automatically applies the index to - # all the nested fields. - # - # There are also methods for helping to build form tags in - # link:classes/ActionView/Helpers/FormOptionsHelper.html, - # link:classes/ActionView/Helpers/DateHelper.html, and - # link:classes/ActionView/Helpers/ActiveRecordHelper.html + # That's how you tipically work with resources. module FormHelper extend ActiveSupport::Concern -- cgit v1.2.3 From b7d8f5a3299788fe8aa152c542e27375ec97a81b Mon Sep 17 00:00:00 2001 From: Xavier Noria Date: Sat, 10 Apr 2010 16:53:25 -0700 Subject: removes some duplication in previous edit --- actionpack/lib/action_view/helpers/form_helper.rb | 5 ----- 1 file changed, 5 deletions(-) (limited to 'actionpack') diff --git a/actionpack/lib/action_view/helpers/form_helper.rb b/actionpack/lib/action_view/helpers/form_helper.rb index 92ec6ee632..192b365bee 100644 --- a/actionpack/lib/action_view/helpers/form_helper.rb +++ b/actionpack/lib/action_view/helpers/form_helper.rb @@ -11,11 +11,6 @@ module ActionView # Form helpers are designed to make working with resources much easier # compared to using vanilla HTML. # - # There are methods to generate all kinds of input fields and the form - # element itself. They get convenient names, IDs, endpoints, etc. so that - # you can work at the model level. Thanks to conventions in the HTML they - # generate controllers receive form data nicely structured in +params+. - # # Model-based forms 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 -- cgit v1.2.3 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(-) (limited to 'actionpack') 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(-) (limited to 'actionpack') 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 From 5b89fd07b28bcd342f874ab08a03efd864db1340 Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Sun, 11 Apr 2010 04:39:25 -0300 Subject: Make namespace work with options[:to] [#4351 state:committed] MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: José Valim --- actionpack/lib/action_dispatch/routing/mapper.rb | 8 ++++++++ actionpack/test/dispatch/routing_test.rb | 4 ++-- 2 files changed, 10 insertions(+), 2 deletions(-) (limited to 'actionpack') diff --git a/actionpack/lib/action_dispatch/routing/mapper.rb b/actionpack/lib/action_dispatch/routing/mapper.rb index 7035e360ec..3e15caee2d 100644 --- a/actionpack/lib/action_dispatch/routing/mapper.rb +++ b/actionpack/lib/action_dispatch/routing/mapper.rb @@ -55,6 +55,14 @@ module ActionDispatch path = args.first end + if @scope[:module] && options[:to] + if options[:to].to_s.include?("#") + options[:to] = "#{@scope[:module]}/#{options[:to]}" + elsif @scope[:controller].nil? + options[:to] = "#{@scope[:module]}##{options[:to]}" + end + end + path = normalize_path(path) if using_match_shorthand?(path, options) diff --git a/actionpack/test/dispatch/routing_test.rb b/actionpack/test/dispatch/routing_test.rb index d38c48bfd4..411a28d8ee 100644 --- a/actionpack/test/dispatch/routing_test.rb +++ b/actionpack/test/dispatch/routing_test.rb @@ -140,7 +140,7 @@ class TestRoutingMapper < ActionDispatch::IntegrationTest namespace :account do match 'shorthand' - match 'description', :to => "account#description", :as => "description" + match 'description', :to => "description", :as => "description" resource :subscription, :credit, :credit_card root :to => "account#index" @@ -864,7 +864,7 @@ class TestRoutingMapper < ActionDispatch::IntegrationTest with_test_routes do assert_equal '/account', account_root_path get '/account' - assert_equal 'account#index', @response.body + assert_equal 'account/account#index', @response.body end end -- cgit v1.2.3