【问题标题】:Rails, PaperClip, Rabl, AngularJS and path to imageRails、PaperClip、Rabl、AngularJS 和图像路径
【发布时间】:2015-08-27 05:21:12
【问题描述】:

所以我在后端有带有 Paperclip 和 Rabl 的 Rails。在前端,我有 Angular。 在 rabl 中,我附加了照片的 url,我想在前端显示什么。

所以在前端我有这个链接
"/public/system/photos/photos/000/000/002/thumbnail/front.jpg"

当我尝试通过 src 显示图像时。

我在响应时收到 404 错误,并且在 rails 控制台中没有路由匹配 [GET] "/public/system/photos/photos/000/000/002/thumbnail/front.jpg"。

我不知道我做错了什么。

如何访问这些静态内容?

【问题讨论】:

    标签: ruby-on-rails angularjs


    【解决方案1】:

    您需要构建自己的端点来下载 url... 在您的控制器中,您需要执行以下操作:

    def download
      instance = YourModel.find(params[:id])
      send_file(instance.file.path, type: instance.file_content_type, disposition: 'inline')
    end
    

    file 是你的回形针属性

    在你的 routes.rb

    get '/your_controller/:id/download', to: 'your_controller#download', as: :download

    因此,您可以在浏览器中粘贴 url:http://localhost:3000/your_controller/xxx/download 以获取文件。

    在你的 Rabl 看来...

    node(:my_url) do |instance|
      download_url(instance.id)
    end
    

    如果您的路线中只有as: :download 选项,download_url 将起作用。

    要使用角度显示图像,请使用ng-src 而不是src。看看Use of ng-src vs src

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-07-31
      • 1970-01-01
      • 2015-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-18
      相关资源
      最近更新 更多