Skip to content
Browse files

Added that respond_to blocks will automatically set the content type …

…to be the same as is requested [DHH]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5131 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent 98a412a commit caa8a005202b267b75baf521d5f613f3a2bce600 @dhh dhh committed Sep 17, 2006
View
8 actionpack/CHANGELOG
@@ -1,5 +1,13 @@
*SVN*
+* Added that respond_to blocks will automatically set the content type to be the same as is requested [DHH]. Examples:
+
+ respond_to do |format|
+ format.html { render :text => "I'm being sent as text/html" }
+ format.rss { render :text => "I'm being sent as application/rss+xml" }
+ format.atom { render :text => "I'm being sent as application/xml", :content_type => Mime::XML }
+ end
+
* Added utf-8 as the default charset for all renders. You can change this default using ActionController::Base.default_charset=(encoding) [DHH]
* Added proper getters and setters for content type and charset [DHH]. Example of what we used to do:
View
7 actionpack/lib/action_controller/mime_responds.rb
@@ -108,7 +108,7 @@ def respond_to(*types, &block)
class Responder #:nodoc:
DEFAULT_BLOCKS = [:html, :js, :xml].inject({}) do |blocks, ext|
- blocks.update ext => %(Proc.new { render :action => "\#{action_name}.r#{ext}" })
+ blocks.update ext => %(Proc.new { render :action => "\#{action_name}.r#{ext}", :content_type => Mime::#{ext.to_s.upcase} })
end
def initialize(block_binding)
@@ -129,7 +129,10 @@ def custom(mime_type, &block)
@order << mime_type
if block_given?
- @responses[mime_type] = block
+ @responses[mime_type] = Proc.new do
+ eval "response.content_type = Mime::#{mime_type.to_sym.to_s.upcase}", @block_binding
+ block.call
+ end
else
if source = DEFAULT_BLOCKS[mime_type.to_sym]
@responses[mime_type] = eval(source, @block_binding)
View
31 actionpack/test/controller/content_type_test.rb
@@ -30,6 +30,15 @@ def render_change_for_rxml
render :action => "render_default_for_rxml"
end
+ def render_default_content_types_for_respond_to
+ respond_to do |format|
+ format.html { render :text => "hello world!" }
+ format.xml { render :action => "render_default_content_types_for_respond_to.rhtml" }
+ format.js { render :text => "hello world!" }
+ format.rss { render :text => "hello world!", :content_type => Mime::XML }
+ end
+ end
+
def rescue_action(e) raise end
end
@@ -96,4 +105,26 @@ def test_change_for_rxml
assert_equal Mime::HTML, @response.content_type
assert_equal "utf-8", @response.charset
end
+
+ def test_render_default_content_types_for_respond_to
+ @request.env["HTTP_ACCEPT"] = Mime::HTML.to_s
+ get :render_default_content_types_for_respond_to
+ assert_equal Mime::HTML, @response.content_type
+
+ @request.env["HTTP_ACCEPT"] = Mime::JS.to_s
+ get :render_default_content_types_for_respond_to
+ assert_equal Mime::JS, @response.content_type
+ end
+
+ def test_render_default_content_types_for_respond_to_with_template
+ @request.env["HTTP_ACCEPT"] = Mime::XML.to_s
+ get :render_default_content_types_for_respond_to
+ assert_equal Mime::XML, @response.content_type
+ end
+
+ def test_render_default_content_types_for_respond_to_with_overwrite
+ @request.env["HTTP_ACCEPT"] = Mime::RSS.to_s
+ get :render_default_content_types_for_respond_to
+ assert_equal Mime::XML, @response.content_type
+ end
end
View
1 actionpack/test/fixtures/content_type/render_default_content_types_for_respond_to.rhtml
@@ -0,0 +1 @@
+<hello>world</hello>

0 comments on commit caa8a00

Please sign in to comment.
Something went wrong with that request. Please try again.