我有一个用户模型,它为一个名为 profile_img 的字段安装了一个载波 uploader 。代码如下:
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/