1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Fix render :json => nil [#2589 state:resolved]

This commit is contained in:
Jeremy Kemper 2009-04-30 16:34:00 -07:00
parent 00d1a57e9f
commit f0b9e28619
2 changed files with 12 additions and 1 deletions

View file

@ -253,7 +253,8 @@ module ActionController
response.content_type ||= Mime::JS response.content_type ||= Mime::JS
render_for_text(js) render_for_text(js)
elsif json = options[:json] elsif options.include?(:json)
json = options[:json]
json = ActiveSupport::JSON.encode(json) unless json.respond_to?(:to_str) json = ActiveSupport::JSON.encode(json) unless json.respond_to?(:to_str)
json = "#{options[:callback]}(#{json})" unless options[:callback].blank? json = "#{options[:callback]}(#{json})" unless options[:callback].blank?
response.content_type ||= Mime::JSON response.content_type ||= Mime::JSON

View file

@ -194,6 +194,10 @@ class TestController < ActionController::Base
render :inline => "<%= controller_name %>" render :inline => "<%= controller_name %>"
end end
def render_json_nil
render :json => nil
end
def render_json_hello_world def render_json_hello_world
render :json => ActiveSupport::JSON.encode(:hello => 'world') render :json => ActiveSupport::JSON.encode(:hello => 'world')
end end
@ -874,6 +878,12 @@ class RenderTest < ActionController::TestCase
assert_equal "The secret is in the sauce\n", @response.body assert_equal "The secret is in the sauce\n", @response.body
end end
def test_render_json_nil
get :render_json_nil
assert_equal 'null', @response.body
assert_equal 'application/json', @response.content_type
end
def test_render_json def test_render_json
get :render_json_hello_world get :render_json_hello_world
assert_equal '{"hello":"world"}', @response.body assert_equal '{"hello":"world"}', @response.body