【问题标题】:carrierwave double key in serializable_hash可序列化哈希中的carrierwave双键
【发布时间】:2015-02-13 10:09:50
【问题描述】:

我有一个用户模型,它为名为 profile_img 的字段安装载波上传器。代码如下:

class User < ActiveRecord::Base
    mount_uploader :profile_img, ProfileUploader
end

profile_img 字段的输出是这样的:

"profile_img": {
            "url": "https://halo-img-dev.s3.amazonaws.com/uploads/user/profile_img/1000/cola_iOS2_512_dribbble2.png",
            "thumb": {
                "url": "https://halo-img-dev.s3.amazonaws.com/uploads/user/profile_img/1000/thumb_cola_iOS2_512_dribbble2.png"
            },
            "medium": {
                "url": "https://halo-img-dev.s3.amazonaws.com/uploads/user/profile_img/1000/medium_cola_iOS2_512_dribbble2.png"
            }
        }

当我尝试通过简单地调用 super 来自定义序列化哈希时,

def serializable_hash(options = {})
    super(options)
end

profile_image 字段键被复制

"profile_img": {
        "profile_img": {  #### duplicate here
            "url": "https://halo-img-dev.s3.amazonaws.com/uploads/user/profile_img/1000/cola_iOS2_512_dribbble2.png",
            "thumb": {
                "url": "https://halo-img-dev.s3.amazonaws.com/uploads/user/profile_img/1000/thumb_cola_iOS2_512_dribbble2.png"
            },
            "medium": {
                "url": "https://halo-img-dev.s3.amazonaws.com/uploads/user/profile_img/1000/medium_cola_iOS2_512_dribbble2.png"
            }
        }
    },

我怀疑这个问题源于载波序列化方法,但找不到解决方案。

有什么线索吗?

【问题讨论】:

    标签: serialization rails-activerecord carrierwave jsonserializer


    【解决方案1】:

    解决方案:

    1. jBuilder

      json.profile_img @user.profile_img.serializable_hash
      
    2. ActiveModelSerializer

      # attributes
      def profile_img
        object.profile_img.serializable_hash
      end
      

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-16
      • 2013-06-25
      • 1970-01-01
      • 1970-01-01
      • 2013-04-17
      • 1970-01-01
      • 2012-02-19
      • 2013-06-21
      相关资源
      最近更新 更多