Changeset 5694
- Timestamp:
- 12/06/06 22:27:08 (2 years ago)
- Files:
-
- trunk/actionpack/CHANGELOG (modified) (1 diff)
- trunk/actionpack/lib/action_controller/base.rb (modified) (3 diffs)
- trunk/actionpack/lib/action_controller/mime_types.rb (modified) (1 diff)
- trunk/actionpack/lib/action_controller/request.rb (modified) (1 diff)
- trunk/actionpack/test/controller/mime_responds_test.rb (modified) (2 diffs)
- trunk/actionpack/test/controller/render_test.rb (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/actionpack/CHANGELOG
r5685 r5694 1 1 *SVN* 2 3 * respond_to recognizes JSON. render :json => @person.to_json automatically sets the content type and takes a :callback option to specify a client-side function to call using the rendered JSON as an argument. #4185 [Scott Raymond, eventualbuddha] 4 # text/x-json response with body 'Element.show({:name: "David"})' 5 respond_to do |format| 6 format.json { render :json => { :name => "David" }.to_json, :callback => 'Element.show' } 7 end 2 8 3 9 * Makes :discard_year work without breaking multi-attribute parsing in AR. #1260, #3800 [sean@ardismg.com, jmartin@desertflood.com, stephen@touset.org, Bob Silva] trunk/actionpack/lib/action_controller/base.rb
r5635 r5694 651 651 # _Deprecation_ _notice_: This used to have the signature <tt>render_text("text", status = 200)</tt> 652 652 # 653 # === Rendering JSON 654 # 655 # Rendering JSON sets the content type to text/x-json and optionally wraps the JSON in a callback. It is expected 656 # that the response will be eval'd for use as a data structure. 657 # 658 # # Renders '{name: "David"}' 659 # render :json => {:name => "David"}.to_json 660 # 661 # Sometimes the result isn't handled directly by a script (such as when the request comes from a SCRIPT tag), 662 # so the callback option is provided for these cases. 663 # 664 # # Renders 'show({name: "David"})' 665 # render :json => {:name => "David"}.to_json, :callback => 'show' 666 # 653 667 # === Rendering an inline template 654 668 # … … 734 748 elsif xml = options[:xml] 735 749 render_xml(xml, options[:status]) 750 751 elsif json = options[:json] 752 render_json(json, options[:callback], options[:status]) 753 754 elsif yaml = options[:yaml] 755 render_yaml(yaml, options[:status]) 736 756 737 757 elsif partial = options[:partial] … … 813 833 response.content_type = Mime::XML 814 834 render_text(xml, status) 835 end 836 837 def render_json(json, callback = nil, status = nil) #:nodoc: 838 json = "#{callback}(#{json})" unless callback.blank? 839 840 response.content_type = Mime::JSON 841 render_text(json, status) 842 end 843 844 def render_yaml(yaml, status = nil) #:nodoc: 845 response.content_type = Mime::YAML 846 render_text(yaml, status) 815 847 end 816 848 trunk/actionpack/lib/action_controller/mime_types.rb
r5663 r5694 3 3 Mime::Type.register "text/html", :html, %w( application/xhtml+xml ), %w( xhtml ) 4 4 Mime::Type.register "text/javascript", :js, %w( application/javascript application/x-javascript ) 5 Mime::Type.register "text/x-json", :json, %w( application/x-json ) 5 6 Mime::Type.register "text/calendar", :ics 6 7 Mime::Type.register "text/csv", :csv trunk/actionpack/lib/action_controller/request.rb
r5664 r5694 67 67 when 'xml' 68 68 content_type = 'application/xml' 69 when 'json' 70 content_type = 'application/x-json' 69 71 end 70 72 end trunk/actionpack/test/controller/mime_responds_test.rb
r5663 r5694 18 18 type.js { render :text => "JS" } 19 19 type.all { render :text => "Nothing" } 20 end 21 end 22 23 def json_or_yaml 24 respond_to do |type| 25 type.json { render :text => "JSON" } 26 type.yaml { render :yaml => "YAML" } 20 27 end 21 28 end … … 163 170 get :just_xml 164 171 assert_response 406 172 end 173 174 def test_json_or_yaml 175 get :json_or_yaml 176 assert_equal 'JSON', @response.body 177 178 @request.env["HTTP_ACCEPT"] = "text/yaml" 179 get :json_or_yaml 180 assert_equal 'YAML', @response.body 181 182 @request.env["HTTP_ACCEPT"] = "text/x-json" 183 get :json_or_yaml 184 assert_equal 'JSON', @response.body 165 185 end 166 186 trunk/actionpack/test/controller/render_test.rb
r5253 r5694 38 38 def render_text_hello_world 39 39 render_text "hello world" 40 end 41 42 def render_json_hello_world 43 render_json({:hello => 'world'}.to_json) 44 end 45 46 def render_json_hello_world_with_callback 47 render_json({:hello => 'world'}.to_json, 'alert') 40 48 end 41 49 … … 164 172 assert_equal "hello world", @response.body 165 173 end 174 175 def test_do_with_render_json 176 get :render_json_hello_world 177 assert_equal '{hello: "world"}', @response.body 178 end 179 180 def test_do_with_render_json_with_callback 181 get :render_json_hello_world_with_callback 182 assert_equal 'alert({hello: "world"})', @response.body 183 end 166 184 167 185 def test_do_with_render_custom_code