Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fix issue with rendering partials and then yielding inside a render l…

…ayout call

The given block was never being executed when another render :partial call
existed before the yield call, due to the block being overriden.

This commit also adds some more tests to render with :layout option.

Signed-off-by: José Valim <jose.valim@gmail.com>
  • Loading branch information...
commit 3e86e4b34ecac0bbd9fab33a459b55958214f3e7 1 parent 55a8bd6
Carlos Antonio da Silva carlosantoniodasilva authored josevalim committed
22 actionpack/lib/action_view/render/partials.rb
View
@@ -30,31 +30,31 @@ module ActionView
#
# By default PartialRenderer uses the template name for the local name of the object passed into the template.
# These examples are effectively the same:
- #
+ #
# <%= render :partial => "contract", :locals => { :contract => @contract } %>
- #
+ #
# <%= render :partial => "contract" %>
#
# By specifying the :as option we can change the way the local variable is namedin the template.
# These examples are effectively the same:
- #
+ #
# <%= render :partial => "contract", :as => :agreement
- #
+ #
# <%= render :partial => "contract", :locals => { :agreement => @contract }
#
# The :object option can be used to directly specify which object is rendered into the partial.
- #
+ #
# Revisiting a previous example we could have written this code.
- #
+ #
# <%= render :partial => "account", :object => @buyer %>
#
# <% for ad in @advertisements %>
# <%= render :partial => "ad", :object => ad %>
# <% end %>
- #
+ #
# The :object and :as options can be used together. We might have a partial which we have named genericly,
# such as 'form'. Using :object and :as together helps us.
- #
+ #
# <%= render :partial => "form", :object => @contract, :as => :contract %>
# == Rendering a collection of partials
#
@@ -70,7 +70,7 @@ module ActionView
# +partial_name_counter+. In the case of the example above, the template would be fed +ad_counter+.
#
# The :as option may be used when rendering partials.
- #
+ #
# Also, you can specify a partial which will be render as a spacer between each element by passing partial name to
# +:spacer_template+. The following example will render "advertiser/_ad_divider.erb" between each ad partial.
#
@@ -323,13 +323,13 @@ def collection_without_template(collection_paths = @collection_paths)
end
def render_partial(object = @object)
- locals, view, template = @locals, @view, @template
+ locals, view, template, block = @locals, @view, @template, @block
object ||= locals[template.variable_name]
locals[@options[:as] || template.variable_name] = object
template.render(view, locals) do |*name|
- view._layout_for(*name, &@block)
+ view._layout_for(*name, &block)
end
end
2  actionpack/test/fixtures/layouts/_partial_and_yield.erb
View
@@ -0,0 +1,2 @@
+<%= render :partial => 'test/partial' %>
+<%= yield %>
1  actionpack/test/fixtures/layouts/_yield_only.erb
View
@@ -0,0 +1 @@
+<%= yield %>
1  actionpack/test/fixtures/layouts/_yield_with_params.erb
View
@@ -0,0 +1 @@
+<%= yield 'Yield!' %>
2  actionpack/test/fixtures/layouts/yield_with_render_partial_inside.erb
View
@@ -0,0 +1,2 @@
+<%= render :partial => 'test/partial' %>
+<%= yield %>
19 actionpack/test/template/render_test.rb
View
@@ -238,6 +238,25 @@ def test_render_with_layout_which_has_render_inline
@view.render(:file => "test/hello_world.erb", :layout => "layouts/yield_with_render_inline_inside")
end
+ def test_render_with_layout_which_renders_another_partial
+ assert_equal %(partial html\nHello world!\n),
+ @view.render(:file => "test/hello_world.erb", :layout => "layouts/yield_with_render_partial_inside")
+ end
+
+ def test_render_layout_with_block_and_yield
+ assert_equal %(Content from block!\n),
+ @view.render(:layout => "layouts/yield_only") { "Content from block!" }
+ end
+
+ def test_render_layout_with_block_and_yield_with_params
+ assert_equal %(Yield! Content from block!\n),
+ @view.render(:layout => "layouts/yield_with_params") { |param| "#{param} Content from block!" }
+ end
+
+ def test_render_layout_with_block_which_renders_another_partial_and_yields
+ assert_equal %(partial html\nContent from block!\n),
+ @view.render(:layout => "layouts/partial_and_yield") { "Content from block!" }
+ end
# TODO: Move to deprecated_tests.rb
def test_render_with_nested_layout_deprecated
Please sign in to comment.
Something went wrong with that request. Please try again.