Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed CaptureHelper#content_for to work with the optional content par…

…ameter instead of just the block #9434 [sandofsky/wildchild]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7522 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit 415649760226a7987ba1f8d234c1785d0fa333b0 1 parent 0d5a7d1
@dhh dhh authored
View
2  actionpack/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Fixed CaptureHelper#content_for to work with the optional content parameter instead of just the block #9434 [sandofsky/wildchild].
+
* Added Mime::Type.register_alias for dealing with different formats using the same mime type [DHH]. Example:
class PostsController < ApplicationController
View
12 actionpack/lib/action_view/helpers/capture_helper.rb
@@ -93,7 +93,7 @@ def capture(*args, &block)
# That will place <script> tags for Prototype, Scriptaculous, and application.js (if it exists)
# on the page; this technique is useful if you'll only be using these scripts in a few views.
#
- # Also, note that content_for concatenates the blocks it is given for a particular
+ # Note that content_for concatenates the blocks it is given for a particular
# identifier in order. For example:
#
# <% content_for :navigation do %>
@@ -109,7 +109,11 @@ def capture(*args, &block)
# Then, in another template or layout, this code would render both links in order:
#
# <ul><%= yield :navigation %></ul>
- #
+ #
+ # Lastly, simple content can be passed as a parameter:
+ #
+ # <% content_for :script, javascript_include_tag(:defaults) %>
+ #
# WARNING: content_for is ignored in caches. So you shouldn't use it
# for elements that will be fragment cached.
#
@@ -118,7 +122,9 @@ def capture(*args, &block)
# would be avaiable as <tt><%= @content_for_footer %></tt>. The preferred usage is now
# <tt><%= yield :footer %></tt>.
def content_for(name, content = nil, &block)
- eval "@content_for_#{name} = (@content_for_#{name} || '') + capture(&block)"
+ existing_content_for = instance_variable_get("@content_for_#{name}").to_s
+ new_content_for = existing_content_for + (block_given? ? capture(&block) : content)
+ instance_variable_set("@content_for_#{name}", new_content_for)
end
private
View
20 actionpack/test/controller/capture_test.rb
@@ -8,6 +8,14 @@ def content_for
render :layout => "talk_from_action"
end
+ def content_for_with_parameter
+ render :layout => "talk_from_action"
+ end
+
+ def content_for_concatenated
+ render :layout => "talk_from_action"
+ end
+
def erb_content_for
render :layout => "talk_from_action"
end
@@ -49,8 +57,18 @@ def test_content_for
assert_equal expected_content_for_output, @response.body
end
+ def test_should_concatentate_content_for
+ get :content_for_concatenated
+ assert_equal expected_content_for_output, @response.body
+ end
+
def test_erb_content_for
- get :content_for
+ get :erb_content_for
+ assert_equal expected_content_for_output, @response.body
+ end
+
+ def test_should_set_content_for_with_parameter
+ get :content_for_with_parameter
assert_equal expected_content_for_output, @response.body
end
Please sign in to comment.
Something went wrong with that request. Please try again.