我有一个用户模型,它为一个名为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"
}
}
},
我怀疑这个问题源于carrierwave序列化方法,但无法找到解决方案。
答案 0 :(得分:0)
解决方案:
<强> JBUILDER 强>
json.profile_img @user.profile_img.serializable_hash
<强> ActiveModelSerializer 强>
# attributes
def profile_img
object.profile_img.serializable_hash
end