我正在使用ruby gem API来处理我的rails应用程序上的图像,这是一个移动后端CarrierWave。有没有办法将图片url和拇指url直接存储在父对象上?
这是默认行为,显示在post对象的json中。注意嵌套的JSON:
{
created_at: "2012-11-17T18:24:04Z",
description: "this is the content",
id: 6,
user_id: 1,
picture: {
url: "/uploads/entry/picture/6/wtf_llama.jpeg",
thumb: {
url: "/uploads/entry/picture/6/thumb_wtf_llama.jpeg"
}
},
updated_at: "2012-11-26T08:16:43Z"
}我希望看到的是:
{
created_at: "2012-11-17T18:24:04Z",
description: "this is the content",
id: 6,
user_id: 1,
picture_url = "/uploads/entry/picture/6/wtf_llama.jpeg",
thumb_url = "/uploads/entry/picture/6/thumb_wtf_llama.jpeg"
updated_at: "2012-11-26T08:16:43Z"
}这个是可能的吗?
发布于 2012-11-27 17:26:30
为什么要在模型中存储这些路径?改进响应(视图或控制器),而不是持久层(模型)。我相信这是使用as_json最容易实现的。可以将picture_url方法添加到模型中,也可以将其他条目合并到最终的散列中。
https://stackoverflow.com/questions/13579949
复制相似问题