ruby-on-rails - Rails4 + 回形针 : Url and Path not matching

标签 ruby-on-rails ruby image get paperclip

我使用回形针上传用户头像。图像正确存储在/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/

相关文章:

ruby - 学习Ruby的好地方是什么?它主要用于什么?

php - 无法显示存储在数据库中的 BLOB

ruby-on-rails - 为什么 Rails 4.2.6 uniqueness : true, case_sensitive : true fail?

ruby-on-rails - rails :Best way to update multiple models when changes are made to another model

jquery - 响应 jquery 请求时出现 304 错误

ruby-on-rails - github-pages 的 bundle 更新失败

ruby - 为什么 ruby​​ 中的 'eval' 这么慢?

ruby - Emacs ruby​​ 模式 if 表达式缩进

css - 并排堆叠两张带有标题的图片并在 Wordpress 中居中

jquery - 如何解决 chrome 中图像闪烁的问题?