diff options
Diffstat (limited to 'actionpack/lib/action_view/helpers')
-rw-r--r-- | actionpack/lib/action_view/helpers/form_helper.rb | 21 |
1 files changed, 15 insertions, 6 deletions
diff --git a/actionpack/lib/action_view/helpers/form_helper.rb b/actionpack/lib/action_view/helpers/form_helper.rb index d2835a2438..4d1e913fe3 100644 --- a/actionpack/lib/action_view/helpers/form_helper.rb +++ b/actionpack/lib/action_view/helpers/form_helper.rb @@ -66,6 +66,9 @@ module ActionView # # <input type="text" id="person_1_name" name="person[1][name]" value="<%= @person.name %>" /> # + # An <tt>index</tt> option may also be passed to <tt>form_for</tt> and <tt>fields_for</tt>. This automatically applies + # the <tt>index</tt> 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 module FormHelper @@ -644,12 +647,13 @@ module ActionView def initialize(object_name, object, template, options, proc) @object_name, @object, @template, @options, @proc = object_name, object, template, options, proc + @default_options = @options ? @options.slice(:index) : {} end (field_helpers - %w(label check_box radio_button fields_for)).each do |selector| src = <<-end_src def #{selector}(method, options = {}) - @template.send(#{selector.inspect}, @object_name, method, options.merge(:object => @object)) + @template.send(#{selector.inspect}, @object_name, method, objectify_options(options)) end end_src class_eval src, __FILE__, __LINE__ @@ -668,20 +672,20 @@ module ActionView name = "#{object_name}[#{ActionController::RecordIdentifier.singular_class_name(object)}]" args.unshift(object) end - + @template.fields_for(name, *args, &block) end def label(method, text = nil, options = {}) - @template.label(@object_name, method, text, options.merge(:object => @object)) + @template.label(@object_name, method, text, objectify_options(options)) end def check_box(method, options = {}, checked_value = "1", unchecked_value = "0") - @template.check_box(@object_name, method, options.merge(:object => @object), checked_value, unchecked_value) + @template.check_box(@object_name, method, objectify_options(options), checked_value, unchecked_value) end def radio_button(method, tag_value, options = {}) - @template.radio_button(@object_name, method, tag_value, options.merge(:object => @object)) + @template.radio_button(@object_name, method, tag_value, objectify_options(options)) end def error_message_on(method, prepend_text = "", append_text = "", css_class = "formError") @@ -689,12 +693,17 @@ module ActionView end def error_messages(options = {}) - @template.error_messages_for(@object_name, options.merge(:object => @object)) + @template.error_messages_for(@object_name, objectify_options(options)) end def submit(value = "Save changes", options = {}) @template.submit_tag(value, options.reverse_merge(:id => "#{object_name}_submit")) end + + private + def objectify_options(options) + @default_options.merge(options.merge(:object => @object)) + end end end |