Permalink
Browse files

Reinstate Base#render_to_string. Introduce AbstractController#render_…

…to_string which stringifies render_to_body.
  • Loading branch information...
jeremy committed Apr 21, 2009
1 parent 504d16c commit f49e3441280380ec810d54bd5a77a7e699415efb
@@ -20,7 +20,7 @@ def render(options = {})
self.response_body = render_to_body(options)
end
- # Raw rendering of a template.
+ # Raw rendering of a template to a Rack-compatible body.
# ====
# @option _prefix<String> The template's path prefix
# @option _layout<String> The relative path to the layout template to use
@@ -33,6 +33,16 @@ def render_to_body(options = {})
_render_template(template, options)
end
+ # Raw rendering of a template to a string.
+ # ====
+ # @option _prefix<String> The template's path prefix
+ # @option _layout<String> The relative path to the layout template to use
+ #
+ # :api: plugin
+ def render_to_string(options = {})
+ Rack::Utils.body_to_s(render_to_body(options)).to_ary.join
+ end
+
def _render_template(template, options)
_action_view._render_template_with_layout(template)
end
@@ -306,17 +306,21 @@ def action_name_base(name = action_name)
(name.is_a?(String) ? name.sub(/^#{controller_path}\//, '') : name).to_s
end
- # Renders according to the same rules as <tt>render</tt>, but returns the result in a string instead
- # of sending it as the response body to the browser.
- def render_to_string(options = nil, &block) #:doc:
+ # Same rules as <tt>render</tt>, but returns a Rack-compatible body
+ # instead of sending the response.
+ def render_to_body(options = nil, &block) #:doc:
render(options, &block)
response.body
ensure
response.content_type = nil
erase_render_results
reset_variables_added_to_assigns
end
-
+
+ def render_to_string(options = {})
+ Rack::Utils.body_to_s(render_to_body(options)).to_ary.join
+ end
+
# Clears the rendered results, allowing for another render to be performed.
def erase_render_results #:nodoc:
response.body = []
@@ -387,4 +391,4 @@ def render_for_action(name, layout, options)
render_for_parts(parts, layout, options)
end
end
-end
+end
@@ -56,6 +56,14 @@ def action_with_ivars
def naked_render
render
end
+
+ def rendering_to_body
+ render_to_body "naked_render.erb"
+ end
+
+ def rendering_to_string
+ render_to_string "naked_render.erb"
+ end
end
class TestRenderer < ActiveSupport::TestCase
@@ -73,6 +81,16 @@ class TestRenderer < ActiveSupport::TestCase
result = Me2.process(:naked_render)
assert_equal "Hello from naked_render.erb", result.response_obj[:body]
end
+
+ test "rendering to a rack body" do
+ result = Me2.process(:rendering_to_body)
+ assert_equal "Hello from naked_render.erb", result.response_obj[:body]
+ end
+
+ test "rendering to a string" do
+ result = Me2.process(:rendering_to_string)
+ assert_equal "Hello from naked_render.erb", result.response_obj[:body]
+ end
end
# Test rendering with prefixes

0 comments on commit f49e344

Please sign in to comment.