视图将上传的回形针图像显示为/original/missing.png

时间:2014-07-03 09:53:10

标签: ruby-on-rails ruby-on-rails-3 paperclip

我有产品型号,它是电子商务商店中的项目,并配有回形针,我可以上传产品型号的图像。

当我为产品添加图像时,图像显示在/system/image/../.../.../thumbs和原始图像中,但是当我这样做时

 <% for product in @products %>
    <%= link_to product.image.url(:original)  %>

  <% end %>

当我浏览页面时,它会显示断开的链接(/system/image/missing.png)。与做一组缩略图类似 - 我试图实现将所有产品图像像缩略图一样。 (例如照片库)

其他代码包括模型如下:

class Product < ActiveRecord::Base
  attr_accessible :name, :price, :image

  has_attached_file :image, :styles => { :medium => "238x238>",
                                         :thumb => "100x100>" }

  do_not_validate_attachment_file_type :image

end

控制器:

  def index
   @products = Product.all
  end

2 个答案:

答案 0 :(得分:1)

让Paperclip了解存储/阅读附件的位置。

将以下选项添加到has_attached_file

:url => "/system/:class/:attachment/:id/:style/:basename.:extension",
:path => ":rails_root/public/system/:class/:attachment/:id/:style/:basename.:extension"

答案 1 :(得分:0)

只是回答是有人偶然发现了这一点。

您需要生成回形针迁移。

rails g paperclip模型附件(例如:rails g paperclip用户头像) - “附件”是您在“has_attached_file”中定义的附件。

其次,您需要添加:

attr_accessible :image, :image_file_name, :image_content_type

我做了那个和中提琴!它有效!