提交 fa2a5ae0 编写于 作者: N Neeraj Singh 提交者: José Valim

If a user wants json output then try best to render json output. In such cases...

If a user wants json output then try best to render json output. In such cases prefer kind_of(String) over respond_to?(to_str)

[#5841 state:resolved]
Signed-off-by: NJosé Valim <jose.valim@gmail.com>
上级 66212f69
......@@ -55,7 +55,7 @@ module All
end
add :json do |json, options|
json = json.to_json(options) unless json.respond_to?(:to_str)
json = json.to_json(options) unless json.kind_of?(String)
json = "#{options[:callback]}(#{json})" unless options[:callback].blank?
self.content_type ||= Mime::JSON
self.response_body = json
......
......@@ -599,6 +599,17 @@ def index
end
end
class RenderJsonRespondWithController < RespondWithController
clear_respond_to
respond_to :json
def index
respond_with(resource) do |format|
format.json { render :json => RenderJsonTestException.new('boom') }
end
end
end
class EmptyRespondWithController < ActionController::Base
def index
respond_with(Customer.new("david", 13))
......@@ -914,6 +925,13 @@ def test_block_inside_respond_with_is_rendered
assert_equal "JSON", @response.body
end
def test_render_json_object_responds_to_str_still_produce_json
@controller = RenderJsonRespondWithController.new
@request.accept = "application/json"
get :index, :format => :json
assert_equal %Q{{"message":"boom","error":"RenderJsonTestException"}}, @response.body
end
def test_no_double_render_is_raised
@request.accept = "text/html"
assert_raise ActionView::MissingTemplate do
......
......@@ -194,3 +194,9 @@ def each
a.each { |i| yield i }
end
end
class RenderJsonTestException < Exception
def to_json(options = nil)
return { :error => self.class.name, :message => self.to_str }.to_json
end
end
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册