我正在尝试将我的Rails服务器切换到AngularJS。到目前为止,一切都很顺利,但我不知道如何从剪纸中获取图像url。
我目前收到的数据是:
{
"id":52,
"name":"Samsung-Galaxy",
"cost":500, "brand":"Samsung",
"description":" Lorem Ipsum has been the industry's standard dummy text when ...",
"created_at":"2015-06-02T09:43:10.000Z",
"updated_at":"201509:43:10.000Z",
"avatar_file_name":"382727-samsung-galaxy-s-4-t-mobile.jpg",
"avatar_content_type":"image/jpeg", "avatar_file_size":20829,
"avatar_updated_at":"201502T09:43:10.000Z"
}但是如何在html页面上显示图像。
发布于 2015-06-02 10:12:41
当您在模型中设置剪纸夹时,如下所示:
class Phone
has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" }
end您可以很容易地从您的Phone object,如文档中所述获得不同的图像。
@medium_avatar_url = Phone.first.avatar.url(:medium)当然,您不会通过实例变量发送它,而是将其实现到您的JSON响应中。
快速解决办法是:
@product = Product.find(params[:id])
@product[:avatar_url] = @product.avatar.url(:medium)
respond_with(@product.as_json)但我强烈建议您查看创建者宝石,这将给您提供更多的灵活性。还有一个大铁路可以让你走!
发布于 2015-06-02 10:33:48
向JSON数据再添加一个url文件,将图像实际路径放在生成rails控制器上,如下所示.
@product = Product.find(params[:id])
@product[:url] = @product.avatar.url(:medium) //paperclip method
respond_with(@product.as_json)https://stackoverflow.com/questions/30592956
复制相似问题