Has*_*mad 3 ruby json ruby-on-rails paperclip rails-api
我发送的Json响应是这样的
"ad": {
"id": 3,
"title": "dgdfg",
"description": "kjlj",
"video_file_name": "SampleVideo_1080x720_1mb.mp4",
"thumbnail_file_name": "images.jpeg",
"campaign_id": null,
"duration": null
},
"video_url": "/system/ads/videos/000/000/003/original/SampleVideo_1080x720_1mb.mp4?1448019186"
Run Code Online (Sandbox Code Playgroud)
我希望video_url也与广告对象合并.
我现在发送回复的方式是
render json: {:success=>true, :message=>"Ad detail",:ad=>@ad, :video_url => @ad.video.url}, :status=>200
Run Code Online (Sandbox Code Playgroud)
我如何将其与广告对象合并?
我想发送它
"ad": {
"id": 3,
"title": "dgdfg",
"description": "kjlj",
"video_file_name": "SampleVideo_1080x720_1mb.mp4",
"thumbnail_file_name": "images.jpeg",
"campaign_id": null,
"duration": null,
"video_url": "/system/ads/videos/000/000/003/original/SampleVideo_1080x720_1mb.mp4?1448019186"
}
Run Code Online (Sandbox Code Playgroud)
我的@ad目标是
#<Ad:0x007efc20495f98
id: 3,
title: "dgdfg",
description: "kjlj",
video_file_name: "SampleVideo_1080x720_1mb.mp4",
video_content_type: "video/mp4",
video_file_size: 1055736,
video_updated_at: Fri, 20 Nov 2015 11:33:06 UTC +00:00,
thumbnail_file_name: "images.jpeg",
thumbnail_content_type: "image/jpeg",
thumbnail_file_size: 9962,
thumbnail_updated_at: Fri, 20 Nov 2015 11:33:22 UTC +00:00,
created_at: Fri, 20 Nov 2015 11:33:22 UTC +00:00,
updated_at: Fri, 20 Nov 2015 11:33:22 UTC +00:00,
campaign_id: nil,
duration: nil>
Run Code Online (Sandbox Code Playgroud)
sad*_*605 10
首先合并{:video_url => @ad.video.url }有@ad那么做以下操作:
{:ad => @ad.attributes.merge( :video_url => @ad.video.url )}
Run Code Online (Sandbox Code Playgroud)
所以你的渲染调用如下所示:
render json: {:success=>true, :message=>"Ad detail", ad: @ad.attributes.merge( :video_url => @ad.video.url )}, :status=>200
Run Code Online (Sandbox Code Playgroud)
@ad.attributes.except("created_at",....)如果您不需要活动记录对象的某些属性,则可能需要使用以下代码@ad.
| 归档时间: |
|
| 查看次数: |
6333 次 |
| 最近记录: |