【问题标题】:How to use Paperclip with image_tag?如何使用带有 image_tag 的回形针?
【发布时间】:2012-10-02 02:44:48
【问题描述】:

我一直在多个模型中将 Paperclip 与我的应用程序一起使用,但以下问题给我带来了问题:

在模型中:

has_attached_file :image, :path => (Rails.root + "document_libraries/:id/filename").to_s, 
                          :url => "/document_libraries/:id"

在视图中:

<%= image_tag(@document_library.image.url) if @document_library.image? %>

这会在浏览器中显示“丢失的图像”图形。我在这里做错了什么?我的公共目录中没有图像。我有这个代码的不同模型,它完美地工作:

has_attached_file :file, :path => (Rails.root + "team_photos/:id/:filename").to_s,
                         :url => "/team_photos/:id"

image_tag(@team_photo.file.url)

我已检查 document_libraries 文件夹是否存在于 rails 根目录中,并且我正在使用的特定记录存在实际的 jpg。权限与 team_photo 模型相同,可以正常工作。

【问题讨论】:

    标签: ruby-on-rails-3 paperclip


    【解决方案1】:

    检查差异,缺少一个:

    :path => (Rails.root + "document_libraries/:id/filename").to_s
    

    :path => (Rails.root + "team_photos/:id/:filename").to_s
    

    【讨论】:

    • 那没有成功。此外,为什么相同的 url 方案适用于其他模型?
    • 是的,我现在可以看到了。这不是问题,但是您在路径中的filename 之前留下了:。这就是两者的区别。
    • 我以为就是这样。但是加了冒号之后,还是不显示图片,即使重启了服务器....
    猜你喜欢
    • 1970-01-01
    • 2018-06-13
    • 1970-01-01
    • 1970-01-01
    • 2015-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多