【问题标题】:How to merge another field in object in rails json response如何在rails json响应中合并对象中的另一个字段
【发布时间】:2015-11-25 12:55:49
【问题描述】:

我发送的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"

我希望 video_url 也与广告对象合并。

我现在发送响应的方式是

render json: {:success=>true, :message=>"Ad detail",:ad=>@ad, :video_url => @ad.video.url}, :status=>200

如何将它与广告对象合并?

我想发一下

"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>

【问题讨论】:

    标签: ruby-on-rails ruby json paperclip rails-api


    【解决方案1】:

    首先将{:video_url =&gt; @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 的某些属性,您可能需要在以下代码中使用@ad.attributes.except("created_at",....)

    【讨论】:

    • 它给出了一个错误 NoMethodError: undefined method `merge' for #<0x007efc204c0568>
    【解决方案2】:

    就在render定义要发送的对象之前(注意如果@ad不是哈希,可能应该先转换为哈希):

    #                    ⇓⇓⇓⇓⇓⇓⇓ this depends on what @ad currently is
    object_to_send = @ad.to_hash.merge(video_url: @ad.video.url)
    

    然后:

    render json: { success: true, 
                   message: "Ad detail",
                   ad: object_to_send }, 
           status: 200
    

    【讨论】:

    • 它给出了一个错误 NoMethodError: undefined method `merge' for #<0x007efc204c0568>
    【解决方案3】:

    你可以使用as_json方法,但是你需要一个直接返回URL的方法

    class Ad
      def video_url
        video.url
      end
    end
    

    然后在渲染中

    render json: {
      success: true, 
      message: "Ad detail",
      ad: ad.as_json(
        only: {
          :id, :title, :description, :video_file_name, :thumbnail_file_name, :campaign_id, :duration
        },
        methods: :video_url
      ), 
      status: 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
    

    【讨论】:

      【解决方案4】:

      您可以通过添加以下内容在哈希中添加新的键和值:

      @ad.attributes[:video_url] = @ad.video.url
      

      希望对你有所帮助。

      【讨论】:

        猜你喜欢
        • 2017-11-05
        • 1970-01-01
        • 1970-01-01
        • 2013-11-11
        • 1970-01-01
        • 2019-01-13
        • 2022-11-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多