【发布时间】:2019-01-13 09:39:17
【问题描述】:
我有一个使用支持 json 字段的 mysql db 5.7 的 rails 4.2 应用程序。所以我的用户模型有一个名为 display_pic 的字段,它是一个 json 对象。
class User < ActiveRecord::Base
serialize :display_pic, JSON
....
在 get_user 操作中,我按如下方式呈现用户
def get_user
@u = User.where(...)
render json: { user: @u }
end
问题在于 json 字段 display_pic 没有作为嵌套的 json 对象出现,而是呈现为字符串。我想得到如下回复
{
"user": {
"name": "some name",
"email": "some email",
"display_pic": {
"url": "http://someurl.com",
"width": "400px",
}
}
}
【问题讨论】:
标签: ruby-on-rails json serialization rails-api