Skip to content
This repository
Browse code

Pass a custom form builder on to nested fields_for calls. [#2023 stat…

…us:committed]

Signed-off-by: David Heinemeier Hansson <david@loudthinking.com>
  • Loading branch information...
commit 3d1d422b8be027417cb640416d154171e75f03d1 1 parent 5cda000
Eloy Durán alloy authored dhh committed
2  actionpack/CHANGELOG
... ... @@ -1,5 +1,7 @@
1 1 *Edge*
2 2
  3 +* Fixed that passing a custom form builder would be forwarded to nested fields_for calls #2023 [Eloy Duran/Nate Wiger]
  4 +
3 5 * Added partial scoping to TranslationHelper#translate, so if you call translate(".foo") from the people/index.html.erb template, you'll actually be calling I18n.translate("people.index.foo") [DHH]
4 6
5 7 * Fix a syntax error in current_page?() that was prevent matches against URL's with multiple query parameters #1385, #1868 [chris finne/Andrew White]
132 actionpack/lib/action_view/helpers/form_helper.rb
@@ -5,17 +5,24 @@
5 5
6 6 module ActionView
7 7 module Helpers
8   - # Form helpers are designed to make working with models much easier compared to using just standard HTML
9   - # elements by providing a set of methods for creating forms based on your models. This helper generates the HTML
10   - # for forms, providing a method for each sort of input (e.g., text, password, select, and so on). When the form
11   - # is submitted (i.e., when the user hits the submit button or <tt>form.submit</tt> is called via JavaScript), the form inputs will be bundled into the <tt>params</tt> object and passed back to the controller.
  8 + # Form helpers are designed to make working with models much easier
  9 + # compared to using just standard HTML elements by providing a set of
  10 + # methods for creating forms based on your models. This helper generates
  11 + # the HTML for forms, providing a method for each sort of input
  12 + # (e.g., text, password, select, and so on). When the form is submitted
  13 + # (i.e., when the user hits the submit button or <tt>form.submit</tt> is
  14 + # called via JavaScript), the form inputs will be bundled into the
  15 + # <tt>params</tt> object and passed back to the controller.
12 16 #
13   - # There are two types of form helpers: those that specifically work with model attributes and those that don't.
14   - # This helper deals with those that work with model attributes; to see an example of form helpers that don't work
15   - # with model attributes, check the ActionView::Helpers::FormTagHelper documentation.
  17 + # There are two types of form helpers: those that specifically work with
  18 + # model attributes and those that don't. This helper deals with those that
  19 + # work with model attributes; to see an example of form helpers that don't
  20 + # work with model attributes, check the ActionView::Helpers::FormTagHelper
  21 + # documentation.
16 22 #
17   - # The core method of this helper, form_for, gives you the ability to create a form for a model instance;
18   - # for example, let's say that you have a model <tt>Person</tt> and want to create a new instance of it:
  23 + # The core method of this helper, form_for, gives you the ability to create
  24 + # a form for a model instance; for example, let's say that you have a model
  25 + # <tt>Person</tt> and want to create a new instance of it:
19 26 #
20 27 # # Note: a @person variable will have been created in the controller.
21 28 # # For example: @person = Person.new
@@ -40,17 +47,22 @@ module Helpers
40 47 # <%= submit_tag 'Create' %>
41 48 # <% end %>
42 49 #
43   - # This example will render the <tt>people/_form</tt> partial, setting a local variable called <tt>form</tt> which references the yielded FormBuilder.
44   - #
45   - # The <tt>params</tt> object created when this form is submitted would look like:
  50 + # This example will render the <tt>people/_form</tt> partial, setting a
  51 + # local variable called <tt>form</tt> which references the yielded
  52 + # FormBuilder. The <tt>params</tt> object created when this form is
  53 + # submitted would look like:
46 54 #
47 55 # {"action"=>"create", "controller"=>"persons", "person"=>{"first_name"=>"William", "last_name"=>"Smith"}}
48 56 #
49   - # The params hash has a nested <tt>person</tt> value, which can therefore be accessed with <tt>params[:person]</tt> in the controller.
50   - # If were editing/updating an instance (e.g., <tt>Person.find(1)</tt> rather than <tt>Person.new</tt> in the controller), the objects
51   - # attribute values are filled into the form (e.g., the <tt>person_first_name</tt> field would have that person's first name in it).
  57 + # The params hash has a nested <tt>person</tt> value, which can therefore
  58 + # be accessed with <tt>params[:person]</tt> in the controller. If were
  59 + # editing/updating an instance (e.g., <tt>Person.find(1)</tt> rather than
  60 + # <tt>Person.new</tt> in the controller), the objects attribute values are
  61 + # filled into the form (e.g., the <tt>person_first_name</tt> field would
  62 + # have that person's first name in it).
52 63 #
53   - # If the object name contains square brackets the id for the object will be inserted. For example:
  64 + # If the object name contains square brackets the id for the object will be
  65 + # inserted. For example:
54 66 #
55 67 # <%= text_field "person[]", "name" %>
56 68 #
@@ -58,8 +70,10 @@ module Helpers
58 70 #
59 71 # <input type="text" id="person_<%= @person.id %>_name" name="person[<%= @person.id %>][name]" value="<%= @person.name %>" />
60 72 #
61   - # If the helper is being used to generate a repetitive sequence of similar form elements, for example in a partial
62   - # used by <tt>render_collection_of_partials</tt>, the <tt>index</tt> option may come in handy. Example:
  73 + # If the helper is being used to generate a repetitive sequence of similar
  74 + # form elements, for example in a partial used by
  75 + # <tt>render_collection_of_partials</tt>, the <tt>index</tt> option may
  76 + # come in handy. Example:
63 77 #
64 78 # <%= text_field "person", "name", "index" => 1 %>
65 79 #
@@ -67,14 +81,17 @@ module Helpers
67 81 #
68 82 # <input type="text" id="person_1_name" name="person[1][name]" value="<%= @person.name %>" />
69 83 #
70   - # An <tt>index</tt> option may also be passed to <tt>form_for</tt> and <tt>fields_for</tt>. This automatically applies
71   - # the <tt>index</tt> to all the nested fields.
  84 + # An <tt>index</tt> option may also be passed to <tt>form_for</tt> and
  85 + # <tt>fields_for</tt>. This automatically applies the <tt>index</tt> to
  86 + # all the nested fields.
72 87 #
73   - # There are also methods for helping to build form tags in link:classes/ActionView/Helpers/FormOptionsHelper.html,
74   - # link:classes/ActionView/Helpers/DateHelper.html, and link:classes/ActionView/Helpers/ActiveRecordHelper.html
  88 + # There are also methods for helping to build form tags in
  89 + # link:classes/ActionView/Helpers/FormOptionsHelper.html,
  90 + # link:classes/ActionView/Helpers/DateHelper.html, and
  91 + # link:classes/ActionView/Helpers/ActiveRecordHelper.html
75 92 module FormHelper
76   - # Creates a form and a scope around a specific model object that is used as
77   - # a base for questioning about values for the fields.
  93 + # Creates a form and a scope around a specific model object that is used
  94 + # as a base for questioning about values for the fields.
78 95 #
79 96 # Rails provides succinct resource-oriented form generation with +form_for+
80 97 # like this:
@@ -86,13 +103,15 @@ module FormHelper
86 103 # <%= f.text_field :author %><br />
87 104 # <% end %>
88 105 #
89   - # There, +form_for+ is able to generate the rest of RESTful form parameters
90   - # based on introspection on the record, but to understand what it does we
91   - # need to dig first into the alternative generic usage it is based upon.
  106 + # There, +form_for+ is able to generate the rest of RESTful form
  107 + # parameters based on introspection on the record, but to understand what
  108 + # it does we need to dig first into the alternative generic usage it is
  109 + # based upon.
92 110 #
93 111 # === Generic form_for
94 112 #
95   - # The generic way to call +form_for+ yields a form builder around a model:
  113 + # The generic way to call +form_for+ yields a form builder around a
  114 + # model:
96 115 #
97 116 # <% form_for :person, :url => { :action => "update" } do |f| %>
98 117 # <%= f.error_messages %>
@@ -103,8 +122,8 @@ module FormHelper
103 122 # <% end %>
104 123 #
105 124 # There, the first argument is a symbol or string with the name of the
106   - # object the form is about, and also the name of the instance variable the
107   - # object is stored in.
  125 + # object the form is about, and also the name of the instance variable
  126 + # the object is stored in.
108 127 #
109 128 # The form builder acts as a regular form helper that somehow carries the
110 129 # model. Thus, the idea is that
@@ -137,17 +156,18 @@ module FormHelper
137 156 # In any of its variants, the rightmost argument to +form_for+ is an
138 157 # optional hash of options:
139 158 #
140   - # * <tt>:url</tt> - The URL the form is submitted to. It takes the same fields
141   - # you pass to +url_for+ or +link_to+. In particular you may pass here a
142   - # named route directly as well. Defaults to the current action.
  159 + # * <tt>:url</tt> - The URL the form is submitted to. It takes the same
  160 + # fields you pass to +url_for+ or +link_to+. In particular you may pass
  161 + # here a named route directly as well. Defaults to the current action.
143 162 # * <tt>:html</tt> - Optional HTML attributes for the form tag.
144 163 #
145   - # Worth noting is that the +form_for+ tag is called in a ERb evaluation block,
146   - # not an ERb output block. So that's <tt><% %></tt>, not <tt><%= %></tt>.
  164 + # Worth noting is that the +form_for+ tag is called in a ERb evaluation
  165 + # block, not an ERb output block. So that's <tt><% %></tt>, not
  166 + # <tt><%= %></tt>.
147 167 #
148 168 # Also note that +form_for+ doesn't create an exclusive scope. It's still
149   - # possible to use both the stand-alone FormHelper methods and methods from
150   - # FormTagHelper. For example:
  169 + # possible to use both the stand-alone FormHelper methods and methods
  170 + # from FormTagHelper. For example:
151 171 #
152 172 # <% form_for :person, @person, :url => { :action => "update" } do |f| %>
153 173 # First name: <%= f.text_field :first_name %>
@@ -156,16 +176,16 @@ module FormHelper
156 176 # Admin? : <%= check_box_tag "person[admin]", @person.company.admin? %>
157 177 # <% end %>
158 178 #
159   - # This also works for the methods in FormOptionHelper and DateHelper that are
160   - # designed to work with an object as base, like FormOptionHelper#collection_select
161   - # and DateHelper#datetime_select.
  179 + # This also works for the methods in FormOptionHelper and DateHelper that
  180 + # are designed to work with an object as base, like
  181 + # FormOptionHelper#collection_select and DateHelper#datetime_select.
162 182 #
163 183 # === Resource-oriented style
164 184 #
165   - # As we said above, in addition to manually configuring the +form_for+ call,
166   - # you can rely on automated resource identification, which will use the conventions
167   - # and named routes of that approach. This is the preferred way to use +form_for+
168   - # nowadays.
  185 + # As we said above, in addition to manually configuring the +form_for+
  186 + # call, you can rely on automated resource identification, which will use
  187 + # the conventions and named routes of that approach. This is the
  188 + # preferred way to use +form_for+ nowadays.
169 189 #
170 190 # For example, if <tt>@post</tt> is an existing record you want to edit
171 191 #
@@ -205,8 +225,10 @@ module FormHelper
205 225 #
206 226 # === Customized form builders
207 227 #
208   - # You can also build forms using a customized FormBuilder class. Subclass FormBuilder and override or define some more helpers,
209   - # then use your custom builder. For example, let's say you made a helper to automatically add labels to form inputs.
  228 + # You can also build forms using a customized FormBuilder class. Subclass
  229 + # FormBuilder and override or define some more helpers, then use your
  230 + # custom builder. For example, let's say you made a helper to
  231 + # automatically add labels to form inputs.
210 232 #
211 233 # <% form_for :person, @person, :url => { :action => "update" }, :builder => LabellingFormBuilder do |f| %>
212 234 # <%= f.text_field :first_name %>
@@ -219,16 +241,23 @@ module FormHelper
219 241 #
220 242 # <%= render :partial => f %>
221 243 #
222   - # The rendered template is <tt>people/_labelling_form</tt> and the local variable referencing the form builder is called <tt>labelling_form</tt>.
  244 + # The rendered template is <tt>people/_labelling_form</tt> and the local
  245 + # variable referencing the form builder is called
  246 + # <tt>labelling_form</tt>.
  247 + #
  248 + # The custom FormBuilder class is automatically merged with the options
  249 + # of a nested fields_for call, unless it's explicitely set.
223 250 #
224   - # In many cases you will want to wrap the above in another helper, so you could do something like the following:
  251 + # In many cases you will want to wrap the above in another helper, so you
  252 + # could do something like the following:
225 253 #
226 254 # def labelled_form_for(record_or_name_or_array, *args, &proc)
227 255 # options = args.extract_options!
228 256 # form_for(record_or_name_or_array, *(args << options.merge(:builder => LabellingFormBuilder)), &proc)
229 257 # end
230 258 #
231   - # If you don't need to attach a form to a model instance, then check out FormTagHelper#form_tag.
  259 + # If you don't need to attach a form to a model instance, then check out
  260 + # FormTagHelper#form_tag.
232 261 def form_for(record_or_name_or_array, *args, &proc)
233 262 raise ArgumentError, "Missing block" unless block_given?
234 263
@@ -910,6 +939,11 @@ def fields_for(record_or_name_or_array, *args, &block)
910 939 index = ""
911 940 end
912 941
  942 + if options[:builder]
  943 + args << {} unless args.last.is_a?(Hash)
  944 + args.last[:builder] ||= options[:builder]
  945 + end
  946 +
913 947 case record_or_name_or_array
914 948 when String, Symbol
915 949 if nested_attributes_association?(record_or_name_or_array)
41 actionpack/test/template/form_helper_test.rb
@@ -1001,6 +1001,47 @@ def test_fields_for_with_labelled_builder
1001 1001 assert_dom_equal expected, output_buffer
1002 1002 end
1003 1003
  1004 + def test_form_for_with_labelled_builder_with_nested_fields_for_without_options_hash
  1005 + klass = nil
  1006 +
  1007 + form_for(:post, @post, :builder => LabelledFormBuilder) do |f|
  1008 + f.fields_for(:comments, Comment.new) do |nested_fields|
  1009 + klass = nested_fields.class
  1010 + ''
  1011 + end
  1012 + end
  1013 +
  1014 + assert_equal LabelledFormBuilder, klass
  1015 + end
  1016 +
  1017 + def test_form_for_with_labelled_builder_with_nested_fields_for_with_options_hash
  1018 + klass = nil
  1019 +
  1020 + form_for(:post, @post, :builder => LabelledFormBuilder) do |f|
  1021 + f.fields_for(:comments, Comment.new, :index => 'foo') do |nested_fields|
  1022 + klass = nested_fields.class
  1023 + ''
  1024 + end
  1025 + end
  1026 +
  1027 + assert_equal LabelledFormBuilder, klass
  1028 + end
  1029 +
  1030 + class LabelledFormBuilderSubclass < LabelledFormBuilder; end
  1031 +
  1032 + def test_form_for_with_labelled_builder_with_nested_fields_for_with_custom_builder
  1033 + klass = nil
  1034 +
  1035 + form_for(:post, @post, :builder => LabelledFormBuilder) do |f|
  1036 + f.fields_for(:comments, Comment.new, :builder => LabelledFormBuilderSubclass) do |nested_fields|
  1037 + klass = nested_fields.class
  1038 + ''
  1039 + end
  1040 + end
  1041 +
  1042 + assert_equal LabelledFormBuilderSubclass, klass
  1043 + end
  1044 +
1004 1045 def test_form_for_with_html_options_adds_options_to_form_tag
1005 1046 form_for(:post, @post, :html => {:id => 'some_form', :class => 'some_class'}) do |f| end
1006 1047 expected = "<form action=\"http://www.example.com\" class=\"some_class\" id=\"some_form\" method=\"post\"></form>"

0 comments on commit 3d1d422

Please sign in to comment.
Something went wrong with that request. Please try again.