aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_view/helpers
diff options
context:
space:
mode:
authorSantiago Pastorino <santiago@wyeworks.com>2012-04-11 19:43:50 -0700
committerSantiago Pastorino <santiago@wyeworks.com>2012-04-11 19:43:50 -0700
commit1db54dffaffb83c7a1dacb4db7e4204c7bd1ddba (patch)
tree5059445ba4132d1ed16c688c7a39870d8707625f /actionpack/lib/action_view/helpers
parente177525d6f6d0f7cf781171ae1c0ef91df7eb4f9 (diff)
parent47cbfbb98a2a4ccb6d434998183f24be4e2bb1c1 (diff)
downloadrails-1db54dffaffb83c7a1dacb4db7e4204c7bd1ddba.tar.gz
rails-1db54dffaffb83c7a1dacb4db7e4204c7bd1ddba.tar.bz2
rails-1db54dffaffb83c7a1dacb4db7e4204c7bd1ddba.zip
Merge pull request #5746 from jmbejar/index_method_form_builder
Add method FormBuilder#index
Diffstat (limited to 'actionpack/lib/action_view/helpers')
-rw-r--r--actionpack/lib/action_view/helpers/form_helper.rb27
1 files changed, 22 insertions, 5 deletions
diff --git a/actionpack/lib/action_view/helpers/form_helper.rb b/actionpack/lib/action_view/helpers/form_helper.rb
index 6219a7a924..3f5829d86a 100644
--- a/actionpack/lib/action_view/helpers/form_helper.rb
+++ b/actionpack/lib/action_view/helpers/form_helper.rb
@@ -673,6 +673,19 @@ module ActionView
# <% end %>
# ...
# <% end %>
+ #
+ # When a collection is used you might want to know the index of each
+ # object into the array. For this purpose, the <tt>index</tt> method
+ # is available in the FormBuilder object.
+ #
+ # <%= form_for @person do |person_form| %>
+ # ...
+ # <%= person_form.fields_for :projects do |project_fields| %>
+ # Project #<%= project_fields.index %>
+ # ...
+ # <% end %>
+ # ...
+ # <% end %>
def fields_for(record_name, record_object = nil, options = {}, &block)
builder = instantiate_builder(record_name, record_object, options)
output = capture(builder, &block)
@@ -1038,7 +1051,7 @@ module ActionView
attr_accessor :object_name, :object, :options
- attr_reader :multipart, :parent_builder
+ attr_reader :multipart, :parent_builder, :index
alias :multipart? :multipart
def multipart=(multipart)
@@ -1076,6 +1089,7 @@ module ActionView
end
end
@multipart = nil
+ @index = options[:index] || options[:child_index]
end
(field_helpers - [:label, :check_box, :radio_button, :fields_for, :hidden_field, :file_field]).each do |selector|
@@ -1107,12 +1121,14 @@ module ActionView
end
index = if options.has_key?(:index)
- "[#{options[:index]}]"
+ options[:index]
elsif defined?(@auto_index)
self.object_name = @object_name.to_s.sub(/\[\]$/,"")
- "[#{@auto_index}]"
+ @auto_index
end
- record_name = "#{object_name}#{index}[#{record_name}]"
+
+ record_name = index ? "#{object_name}[#{index}][#{record_name}]" : "#{object_name}[#{record_name}]"
+ fields_options[:child_index] = index
@template.fields_for(record_name, record_object, fields_options, &block)
end
@@ -1250,7 +1266,8 @@ module ActionView
explicit_child_index = options[:child_index]
output = ActiveSupport::SafeBuffer.new
association.each do |child|
- output << fields_for_nested_model("#{name}[#{explicit_child_index || nested_child_index(name)}]", child, options, block)
+ options[:child_index] = nested_child_index(name) unless explicit_child_index
+ output << fields_for_nested_model("#{name}[#{options[:child_index]}]", child, options, block)
end
output
elsif association