MXV*_*MXV 4 ruby json ruby-on-rails
我有以下代码返回一个包含@ admin_user.companies的一些数据的json:
@admin_user = User.find_by_email(email)
render :json=>{:status=>{:code=>200,:token=>@admin_user.authentication_token,
:user=> @admin_user,
:companies => @admin_user.companies }}
Run Code Online (Sandbox Code Playgroud)
每家公司也有很多"地点".如何在json的@ admin_user.companies中包含每个公司的所有位置?
传统方式是使用
render json: @admin_user.companies, include: :locations
Run Code Online (Sandbox Code Playgroud)
(请参阅#as_json更多选项.)
您不需要在JSON中包含状态代码,因为它已经在HTTP标头中.因此,以下内容可能会使您接近所需.
render :json => @admin_user,
:include => {
:companies => { :include => :locations },
},
:methods => :authentication_token
Run Code Online (Sandbox Code Playgroud)
这只是一个例子.您必须配置:include并:methods获得您想要的内容.要获得更细粒度的控制,请查看JBuilder或RABL.
| 归档时间: |
|
| 查看次数: |
1365 次 |
| 最近记录: |