2017-02-14 47 views
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 

我該怎麼做呢?

+0

不要添加額外的字段,您可以用活動記錄串行實現它。你能更新你的完整的JSON嗎? –

+0

@DipakG。只是更新了代碼。我需要存儲將由客戶端發送給我的'thumbnail' url。我如何用序列化程序實現它? –

回答

0

好吧,我設法通過在我的mac上安裝ffmpeggem 'carrierwave-video-thumbnailer'來完成此操作。

並補充thumb我NewsSerializer

def attachments 
    a=[] 
    object.attachments.each do |att| 
     a << {url: att.url, content_type: att.content_type, thumb: att.thumb} 
    end 
    a 
end