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: José Valim <jose.valim@gmail.com>
This commit is contained in:
parent
66212f69ac
commit
fa2a5ae033
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user