Views shows uploaded paperclip images as /original/missing.png

末鹿安然 提交于 2019-12-11 13:28:34

问题


I have Product model which are items in the e-commerce store and is configured with paperclip to i can upload images for the product model.

When i add the image for the product, the image appears in /system/image/../.../.../thumbs and original but when i do

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

  <% end %>

it appears broken links (/system/image/missing.png) when i browse the page. Similar to doing a group of thumbnails - i am trying to achieve to get all the product images out like a thumbnail. (eg. photo gallery)

Other code include model are below:

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

Controller:

  def index
   @products = Product.all
  end

回答1:


Let the Paperclip know about the location of where to store/read the attachments.

Add the following options to has_attached_file:

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



回答2:


Just to answer is someone stumbles upon this.

You will need to generate the paperclip Migration.

rails g paperclip Model attachment (for example: rails g paperclip User avatar) - the "attachment" is the one you defined in "has_attached_file".

Secondly, you need to add:

attr_accessible :image, :image_file_name, :image_content_type

i did that and viola! it worked!



来源:https://stackoverflow.com/questions/24550464/views-shows-uploaded-paperclip-images-as-original-missing-png

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!