mirror of
https://github.com/github/rails.git
synced 2026-01-30 00:38:00 -05:00
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
This commit is contained in:
@@ -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:
|
||||
|
||||
@@ -108,7 +108,7 @@ module ActionController #:nodoc:
|
||||
|
||||
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 @@ module ActionController #:nodoc:
|
||||
@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 @@ class ContentTypeController < ActionController::Base
|
||||
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 @@ class ContentTypeTest < Test::Unit::TestCase
|
||||
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
|
||||
1
actionpack/test/fixtures/content_type/render_default_content_types_for_respond_to.rhtml
vendored
Normal file
1
actionpack/test/fixtures/content_type/render_default_content_types_for_respond_to.rhtml
vendored
Normal file
@@ -0,0 +1 @@
|
||||
<hello>world</hello>
|
||||
Reference in New Issue
Block a user