diff --git a/actionpack/lib/action_view/renderer/partial_renderer.rb b/actionpack/lib/action_view/renderer/partial_renderer.rb index c7765732c7..e2b5e8e36e 100644 --- a/actionpack/lib/action_view/renderer/partial_renderer.rb +++ b/actionpack/lib/action_view/renderer/partial_renderer.rb @@ -283,7 +283,7 @@ module ActionView end if layout = @options[:layout] - layout = find_template(layout, @locals.keys + [@variable]) + layout = find_template(layout, @locals.keys + [@variable, @variable_counter]) end result = @template ? collection_with_template : collection_without_template @@ -292,6 +292,7 @@ module ActionView locals = @locals result.map! do |content| locals[@variable] = @collection[result.index(content)] + locals[@variable_counter] = result.index(content) layout.render(@view, @locals) { content } end end diff --git a/actionpack/test/fixtures/test/_b_layout_for_partial_with_object_counter.html.erb b/actionpack/test/fixtures/test/_b_layout_for_partial_with_object_counter.html.erb new file mode 100644 index 0000000000..44d6121297 --- /dev/null +++ b/actionpack/test/fixtures/test/_b_layout_for_partial_with_object_counter.html.erb @@ -0,0 +1 @@ +<%= yield %> \ No newline at end of file diff --git a/actionpack/test/template/render_test.rb b/actionpack/test/template/render_test.rb index a76526f946..cdaca56ef2 100644 --- a/actionpack/test/template/render_test.rb +++ b/actionpack/test/template/render_test.rb @@ -238,12 +238,19 @@ module RenderTestCases assert_equal "Hello: AmazonHello: Yahoo", @view.render(:partial => "test/customer", :layout => 'test/b_layout_for_partial', :collection => [ Customer.new("Amazon"), Customer.new("Yahoo") ]) end - def test_render_partial_with_layout_using_collection_and_template_makes_current_item_available_in_template - assert_equal 'Hello: AmazonHello: Yahoo', @view.render(:partial => "test/customer", :layout => 'test/b_layout_for_partial_with_object', :collection => [ Customer.new("Amazon"), Customer.new("Yahoo") ]) + def test_render_partial_with_layout_using_collection_and_template_makes_current_item_available_in_layout + assert_equal 'Hello: AmazonHello: Yahoo', + @view.render(:partial => "test/customer", :layout => 'test/b_layout_for_partial_with_object', :collection => [ Customer.new("Amazon"), Customer.new("Yahoo") ]) end - def test_render_partial_with_layout_using_object_and_template_makes_object_available_in_template - assert_equal 'Hello: Amazon', @view.render(:partial => "test/customer", :layout => 'test/b_layout_for_partial_with_object', :object => Customer.new("Amazon")) + def test_render_partial_with_layout_using_collection_and_template_makes_current_item_counter_available_in_layout + assert_equal 'Hello: AmazonHello: Yahoo', + @view.render(:partial => "test/customer", :layout => 'test/b_layout_for_partial_with_object_counter', :collection => [ Customer.new("Amazon"), Customer.new("Yahoo") ]) + end + + def test_render_partial_with_layout_using_object_and_template_makes_object_available_in_layout + assert_equal 'Hello: Amazon', + @view.render(:partial => "test/customer", :layout => 'test/b_layout_for_partial_with_object', :object => Customer.new("Amazon")) end def test_render_partial_with_empty_array_should_return_nil