1
我有一个News
模型与Carrierwave AttachmentUploader安装。Rails添加字段到carrierwave宝石
JSON的输出是这样的:
{
"id": 158,
"title": "title1",
"description": "desc1",
"category": "ipsum",
"attachments": [
{
"url": "/uploads/news/attachments/158/Tesla_P100D_App_v3.0_Review.mp4",
"content_type": "application/mp4"
}
],
"created_at": "2017-02-14T08:34:14.119Z"
}
我想thumbnail
字段添加到Carrierwave获得电流输出:
{
"id": 158,
"title": "title1",
"description": "desc1",
"category": "ipsum",
"attachments": [
{
"url": "/uploads/news/attachments/158/Tesla_P100D_App_v3.0_Review.mp4",
"content_type": "application/mp4",
"thumbnail": {
"name": "Tesla_P100D_App_v3.0_Review",
"url": "/uploads/news/attachments/158/Tesla_P100D_App_v3.0_Review.png"
}
}
],
"created_at": "2017-02-14T08:34:14.119Z"
}
这里是我的NewsSerializer
:
class NewsSerializer < ActiveModel::Serializer
attributes :id, :title, :description, :latitude, :longitude, :category, :tag_list, :avg_rating, :attachments, :created_at
def attachments
a=[]
object.attachments.each do |att|
a << {url: att.url, content_type: att.content_type}
end
a
end
end
我该怎么做呢?
不要添加额外的字段,您可以用活动记录串行实现它。你能更新你的完整的JSON吗? –
@DipakG。只是更新了代码。我需要存储将由客户端发送给我的'thumbnail' url。我如何用序列化程序实现它? –