提交 72f37bd8 编写于 作者: D Diego Carrion 提交者: José Valim

renderer calls object.to_json when rendering :json => object [#5655 state:resolved]

Signed-off-by: NJosé Valim <jose.valim@gmail.com>
上级 4966b915
......@@ -71,7 +71,7 @@ def self._write_render_options
end
add :json do |json, options|
json = ActiveSupport::JSON.encode(json, options) unless json.respond_to?(:to_str)
json = json.to_json(options) unless json.respond_to?(:to_str)
json = "#{options[:callback]}(#{json})" unless options[:callback].blank?
self.content_type ||= Mime::JSON
self.response_body = json
......
......@@ -9,6 +9,10 @@ def as_json(options={})
hash.except!(*options[:except]) if options[:except]
hash
end
def to_json(options = {})
super :except => [:c, :e]
end
end
class TestController < ActionController::Base
......@@ -49,6 +53,10 @@ def render_json_with_render_to_string
def render_json_with_extra_options
render :json => JsonRenderable.new, :except => [:c, :e]
end
def render_json_without_options
render :json => JsonRenderable.new
end
end
tests TestController
......@@ -109,4 +117,9 @@ def test_render_json_forwards_extra_options
assert_equal '{"a":"b"}', @response.body
assert_equal 'application/json', @response.content_type
end
def test_render_json_calls_to_json_from_object
get :render_json_without_options
assert_equal '{"a":"b"}', @response.body
end
end
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册