我有一个用户模型,该模型为名为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"
            }
        }
    },

我怀疑这个问题源于载波序列化方法,但找不到解决方案。
有什么线索吗?

最佳答案

解决方案:


jBuilder的

json.profile_img @user.profile_img.serializable_hash

ActiveModelSerializer

# attributes
def profile_img
  object.profile_img.serializable_hash
end

关于serialization - serializable_hash中的载波双键,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27481918/

10-09 02:00