Added that respond_to blocks will automatically set the content type to be the...

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
上级 98a412aa
*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:
......
......@@ -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)
......
......@@ -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
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册