Ruby on rails 如何在rails json响应中合并对象中的另一个字段
我发送的Json响应是这样的Ruby on rails 如何在rails json响应中合并对象中的另一个字段,ruby-on-rails,ruby,json,paperclip,rails-api,Ruby On Rails,Ruby,Json,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/o
"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"
我希望视频url也与广告对象合并
我现在发送响应的方式是
render json: {:success=>true, :message=>"Ad detail",:ad=>@ad, :video_url => @ad.video.url}, :status=>200
如何将其与ad对象合并
我想把它寄出去
"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"
}
我的@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>
#
首先将{:video\u url=>@ad.video.url}
与@ad
合并,然后执行以下操作:
{:ad => @ad.attributes.merge( :video_url => @ad.video.url )}
因此,渲染调用如下所示:
render json: {:success=>true, :message=>"Ad detail", ad: @ad.attributes.merge( :video_url => @ad.video.url )}, :status=>200
您可能需要使用
@ad.attributes。如果您不需要活动记录对象@ad
的某些属性,请在渲染之前定义要发送的对象(请注意,如果@ad
不是散列,则可能应该在之前将其转换为散列),则以下代码中的(“创建的”、…)
除外:
然后:
render json: { success: true,
message: "Ad detail",
ad: object_to_send },
status: 200
您可以通过添加以下内容在哈希中添加新的键和值:
@ad.attributes[:video_url] = @ad.video.url
我希望这对您有所帮助。您可以使用as_json
方法,但您需要一个直接返回URL的方法
class Ad
def video_url
video.url
end
end
然后在渲染中
呈现json:{
成功:没错,
信息:“广告详情”,
ad:ad.as_json(
仅:{
:id,:title,:description,:video\u file\u name,:缩略图\u file\u name,:campaign\u id,:duration
},
方法::视频链接
),
现状:200
当然,如果你想,你可以把它包装成某种方法
class Ad
def my_video_json
as_json(
only: {
:id, :title, :description, :video_file_name, :thumbnail_file_name, :campaign_id, :duration
},
methods: :video_url
)
end
end
然后渲染将如下所示
render json: { success: true, message: "Ad detail", ad: ad.my_video_json }, status: 200
它给出了一个错误名称Ror:undefined方法“merge”for#请分享什么类@ad
。@HaseebAhmad:尝试使用@ad.video.path
获取文件系统上文件的完整路径Thankuuuuuu:)…只有一个问题我的问题出了什么问题,所以它被否决了:(Ohhhh Sorryy:)如果你喜欢,请帮我一个忙:)它给出了一个错误名称。错误:未定义的方法“merge”用于#what is@ad?我以为它是散列!更新了我的答案。你需要使用.attributes方法来进行对象散列。谢谢:)……它工作……只有一个问题我的问题出了什么问题,所以它被否决了:(@HaseebAhmad,刚刚注意到如果您不需要活动记录对象的某些属性,您可能需要使用@ad.attributes.except(“created_at”,…)
)。
render json: { success: true, message: "Ad detail", ad: ad.my_video_json }, status: 200