feiyang10086 发表于 2011-11-27 10:30

关于json

关于json







js.erb


Ruby代码1.
2.alert('New object id: ' + <%= @user.id %>);

alert('New object id: ' + <%= @user.id %>);在这个文件里读rails变量到js,实际是可以直接运行rails


标注一个render json需要include和only选择的

Ruby代码1.
2.respond_to do |format|   
3.    format.html # index.html.erb   
4.    format.json{ render :json => @things.to_json(:include => { :photos => { :only => [:id, :url] } }) }   
5.end

respond_to do |format|
    format.html # index.html.erb
    format.json{ render :json => @things.to_json(:include => { :photos => { :only => [:id, :url] } }) }
end这个有个比较cool的做法


Ruby代码1.class Things < ActiveRecord::Base   
2.def as_json(options={})   
3.    super(options || include: :photos, only: [:id, :url])#1.9 hash   
4.end
5.end
6.
7.#然后controller就可以直接用   
8.render :json => @things

2gua 发表于 2011-11-28 08:14

喜欢json格式。
页: [1]
查看完整版本: 关于json