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

Changeset 7905

Show
Ignore:
Timestamp:
10/15/07 07:04:10 (7 months ago)
Author:
nzkoz
Message:

Fix Json related documentation for render and the AR serializer. Closes #9814. Closes #9833. [chuyeow]

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/actionpack/lib/action_controller/base.rb

    r7820 r7905  
    749749      # === Rendering JSON 
    750750      # 
    751       # Rendering JSON sets the content type to text/x-json and optionally wraps the JSON in a callback. It is expected 
    752       # that the response will be eval'd for use as a data structure. 
    753       # 
    754       #   # Renders '{name: "David"}' 
     751      # Rendering JSON sets the content type to application/json and optionally wraps the JSON in a callback. It is expected 
     752      # that the response will be parsed (or eval'd) for use as a data structure. 
     753      # 
     754      #   # Renders '{"name": "David"}' 
    755755      #   render :json => {:name => "David"}.to_json 
    756756      # 
     757      # It's not necessary to call <tt>to_json</tt> on the object you want to render, since <tt>render</tt> will 
     758      # automatically do that for you: 
     759      # 
     760      #   # Also renders '{"name": "David"}' 
     761      #   render :json => {:name => "David"} 
     762      # 
    757763      # Sometimes the result isn't handled directly by a script (such as when the request comes from a SCRIPT tag), 
    758       # so the callback option is provided for these cases. 
    759       # 
    760       #   # Renders 'show({name: "David"})' 
     764      # so the <tt>:callback</tt> option is provided for these cases. 
     765      # 
     766      #   # Renders 'show({"name": "David"})' 
    761767      #   render :json => {:name => "David"}.to_json, :callback => 'show' 
    762768      # 
  • trunk/actionpack/lib/action_controller/mime_type.rb

    r7719 r7905  
    5353      end 
    5454 
    55       # Registers an alias that's not usd on mime type lookup, but can be referenced directly. Especially useful for 
     55      # Registers an alias that's not used on mime type lookup, but can be referenced directly. Especially useful for 
    5656      # rendering different HTML versions depending on the user agent, like an iPhone. 
    5757      def register_alias(string, symbol, extension_synonyms = []) 
  • trunk/activerecord/lib/active_record/serializers/json_serializer.rb

    r7736 r7905  
    11module ActiveRecord #:nodoc: 
    22  module Serialization 
     3    # Returns a JSON string representing the model. Some configuration is 
     4    # available through +options+. 
     5    # 
     6    # Without any +options+, the returned JSON string will include all 
     7    # the model's attributes. For example: 
     8    # 
     9    #   konata = User.find(1) 
     10    #   konata.to_json 
     11    # 
     12    #   {"id": 1, "name": "Konata Izumi", "age": 16, 
     13    #    "created_at": "2006/08/01", "awesome": true} 
     14    # 
     15    # The :only and :except options can be used to limit the attributes 
     16    # included, and work similar to the #attributes method. For example: 
     17    # 
     18    #   konata.to_json(:only => [ :id, :name ]) 
     19    # 
     20    #   {"id": 1, "name": "Konata Izumi"} 
     21    # 
     22    #   konata.to_json(:except => [ :id, :created_at, :age ]) 
     23    # 
     24    #   {"name": "Konata Izumi", "awesome": true} 
     25    # 
     26    # To include any methods on the model, use :methods. 
     27    # 
     28    #   konata.to_json(:methods => :permalink) 
     29    # 
     30    #   {"id": 1, "name": "Konata Izumi", "age": 16, 
     31    #    "created_at": "2006/08/01", "awesome": true, 
     32    #    "permalink": "1-konata-izumi"} 
     33    # 
     34    # To include associations, use :include. 
     35    # 
     36    #   konata.to_json(:include => :posts) 
     37    # 
     38    #   {"id": 1, "name": "Konata Izumi", "age": 16, 
     39    #    "created_at": "2006/08/01", "awesome": true, 
     40    #    "posts": [{"id": 1, "author_id": 1, "title": "Welcome to the weblog"}, 
     41    #              {"id": 2, author_id: 1, "title": "So I was thinking"}]} 
     42    # 
     43    # 2nd level and higher order associations work as well: 
     44    # 
     45    #   konata.to_json(:include => { :posts => { 
     46    #                                  :include => { :comments => { 
     47    #                                                :only => :body } }, 
     48    #                                  :only => :title } }) 
     49    # 
     50    #   {"id": 1, "name": "Konata Izumi", "age": 16, 
     51    #    "created_at": "2006/08/01", "awesome": true, 
     52    #    "posts": [{"comments": [{"body": "1st post!"}, {"body": "Second!"}], 
     53    #               "title": "Welcome to the weblog"}, 
     54    #              {"comments": [{"body": "Don't think too hard"}], 
     55    #               "title": "So I was thinking"}]} 
    356    def to_json(options = {}) 
    457      JsonSerializer.new(self, options).to_s