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。我如何用序列化程序實現它? –