Changeset 7905
- Timestamp:
- 10/15/07 07:04:10 (7 months ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/actionpack/lib/action_controller/base.rb
r7820 r7905 749 749 # === Rendering JSON 750 750 # 751 # Rendering JSON sets the content type to text/x-json and optionally wraps the JSON in a callback. It is expected752 # that the response will be eval'dfor 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"}' 755 755 # render :json => {:name => "David"}.to_json 756 756 # 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 # 757 763 # Sometimes the result isn't handled directly by a script (such as when the request comes from a SCRIPT tag), 758 # so the callbackoption 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"})' 761 767 # render :json => {:name => "David"}.to_json, :callback => 'show' 762 768 # trunk/actionpack/lib/action_controller/mime_type.rb
r7719 r7905 53 53 end 54 54 55 # Registers an alias that's not us d on mime type lookup, but can be referenced directly. Especially useful for55 # Registers an alias that's not used on mime type lookup, but can be referenced directly. Especially useful for 56 56 # rendering different HTML versions depending on the user agent, like an iPhone. 57 57 def register_alias(string, symbol, extension_synonyms = []) trunk/activerecord/lib/active_record/serializers/json_serializer.rb
r7736 r7905 1 1 module ActiveRecord #:nodoc: 2 2 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"}]} 3 56 def to_json(options = {}) 4 57 JsonSerializer.new(self, options).to_s