Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 5694

Show
Ignore:
Timestamp:
12/06/06 22:27:08 (2 years ago)
Author:
bitsweat
Message:

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. References #4185.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/actionpack/CHANGELOG

    r5685 r5694  
    11*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 
    28 
    39* 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  
    651651      # _Deprecation_ _notice_: This used to have the signature <tt>render_text("text", status = 200)</tt> 
    652652      # 
     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      # 
    653667      # === Rendering an inline template 
    654668      # 
     
    734748          elsif xml = options[:xml] 
    735749            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]) 
    736756 
    737757          elsif partial = options[:partial] 
     
    813833        response.content_type = Mime::XML 
    814834        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) 
    815847      end 
    816848 
  • trunk/actionpack/lib/action_controller/mime_types.rb

    r5663 r5694  
    33Mime::Type.register "text/html", :html, %w( application/xhtml+xml ), %w( xhtml ) 
    44Mime::Type.register "text/javascript", :js, %w( application/javascript application/x-javascript ) 
     5Mime::Type.register "text/x-json", :json, %w( application/x-json ) 
    56Mime::Type.register "text/calendar", :ics 
    67Mime::Type.register "text/csv", :csv 
  • trunk/actionpack/lib/action_controller/request.rb

    r5664 r5694  
    6767            when 'xml' 
    6868              content_type = 'application/xml' 
     69            when 'json' 
     70              content_type = 'application/x-json' 
    6971            end 
    7072          end 
  • trunk/actionpack/test/controller/mime_responds_test.rb

    r5663 r5694  
    1818      type.js   { render :text => "JS"      } 
    1919      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" } 
    2027    end 
    2128  end 
     
    163170    get :just_xml 
    164171    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 
    165185  end 
    166186 
  • trunk/actionpack/test/controller/render_test.rb

    r5253 r5694  
    3838  def render_text_hello_world 
    3939    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') 
    4048  end 
    4149 
     
    164172    assert_equal "hello world", @response.body 
    165173  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 
    166184 
    167185  def test_do_with_render_custom_code