我使用回形针上传用户头像。图像正确存储在/public 目录中。但是我不知道如何显示图像。我玩了 :url 和 :path 设置大约一个小时,但无法以图像将在浏览器中显示的方式匹配它们。 在 GET 请求路径中总是有一个 'images/localhost' 我无法摆脱。 这是我的代码:
用户.rb
class User < ActiveRecord::Base
has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "missing.png",
:url => ':class/:id/:style.:extension',
:path => ':url'
validates_attachment_content_type :avatar, :content_type => /\Aimage\/.*\Z/
end
请求查找的 html 标签中的路径如下所示:
<img alt="Original" src="/images/localhost/users/1/original.png?1405249849" />
返回图像的正确请求是
http://localhost:3000/users/1/original.png?1405248482.
如何让请求匹配正确的文件系统路径。或者:我的设置有什么问题?
我没有更改 application.rb 或 development.rb
最好的问候, 凯夫
最佳答案
您应该尽可能使用 paperclip
的 url 或路径助手。所以要显示图像使用:
<%= image_tag @user.avatar.url(:medium) %>
然后: 图片 url 不是文件系统路径。根据您在回形针中使用的存储,图像可以驻留在不同的地方,请参阅 understanding storage回形针 gem 。
如果你使用文件存储,文件存储在类似这样的地方
public/system/users/avatar/000/000/013/small/my_pic.png
关于ruby-on-rails - Rails4 + 回形针 : Url and Path not matching,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24722136/