aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib
diff options
context:
space:
mode:
authorJosé Valim <jose.valim@gmail.com>2011-05-22 04:26:31 -0700
committerJosé Valim <jose.valim@gmail.com>2011-05-22 04:26:31 -0700
commit2cd2f51ded045d7cd69d96dd19ff98446776b8f9 (patch)
tree73948ac985f5163f096970c1893aff650ad362ff /actionpack/lib
parenta6e945554dedf0a33527df2b43c283dd17ae8eb3 (diff)
parent7c562d5e460d97b18e4f3367b3cfb13401732920 (diff)
downloadrails-2cd2f51ded045d7cd69d96dd19ff98446776b8f9.tar.gz
rails-2cd2f51ded045d7cd69d96dd19ff98446776b8f9.tar.bz2
rails-2cd2f51ded045d7cd69d96dd19ff98446776b8f9.zip
Merge pull request #1189 from jmbejar/7c562d5e460d97b18e4f3367b3cfb13401732920
Add method FormHelper#fields_for_with_index
Diffstat (limited to 'actionpack/lib')
-rw-r--r--actionpack/lib/action_view/helpers/form_helper.rb19
1 files changed, 19 insertions, 0 deletions
diff --git a/actionpack/lib/action_view/helpers/form_helper.rb b/actionpack/lib/action_view/helpers/form_helper.rb
index 07e2c8d341..319aa36698 100644
--- a/actionpack/lib/action_view/helpers/form_helper.rb
+++ b/actionpack/lib/action_view/helpers/form_helper.rb
@@ -555,6 +555,19 @@ module ActionView
# ...
# <% end %>
#
+ # In addition, you may want to have access to the current iteration index.
+ # In that case, you can use a similar method called fields_for_with_index
+ # which receives a block with an extra parameter:
+ #
+ # <%= form_for @person do |person_form| %>
+ # ...
+ # <%= person_form.fields_for_with_index :projects do |project_fields, index| %>
+ # Position: <%= index %>
+ # Name: <%= project_fields.text_field :name %>
+ # <% end %>
+ # ...
+ # <% end %>
+ #
# When projects is already an association on Person you can use
# +accepts_nested_attributes_for+ to define the writer method for you:
#
@@ -1216,6 +1229,12 @@ module ActionView
RUBY_EVAL
end
+ def fields_for_with_index(record_name, record_object = nil, fields_options = {}, &block)
+ index = fields_options[:index] || options[:child_index] || nested_child_index(@object_name)
+ block_with_index = Proc.new{ |obj| block.call(obj, index) }
+ fields_for(record_name, record_object, fields_options, &block_with_index)
+ end
+
def fields_for(record_name, record_object = nil, fields_options = {}, &block)
fields_options, record_object = record_object, nil if record_object.is_a?(Hash)
fields_options[:builder] ||= options[:builder]