在回形针上使用多个嵌套图像时的默认图片

发布于 2024-12-06 03:50:09 字数 1359 浏览 2 评论 0原文

我正在使用回形针上传多个项目图像。我遵循了本教程的一些内容 http:// smoothd.com/general/adding-multiple-images-to-a-rails-model-with-paperclip/ 我的模型是:

Item.rb

has_many :item_images, :dependent => :destroy
accepts_nested_attributes_for :item_images, :reject_if => lambda { |l| l['item_image'].nil? }

ItemImage.rb

class ItemImage < ActiveRecord::Base
 belongs_to :item
 belongs_to :user
 #for image. Paperclip     following Bootstrap sizes.
 has_attached_file :image,
                :styles => { :large => ["330x230>",:png], :medium => ["210x150>",:png], :small => ["90x90>",:png], :thumb => ["32x32>", :png] },
                :default_url => '/images/missing_image_:style.png',
                :storage => :s3,
                :bucket => 'iscomp',
                :s3_credentials => "#{RAILS_ROOT}/config/amazon_s3.yml",
                :path => "iscomp/:attachment/:style/:id.:extension"
 validates_attachment_presence :image
 validates_attachment_size :image, :less_than => 5.megabytes
end 

我的问题是,由于图像存储在第二个模型上,当 @item.item_image 上没有图像/记录时,我无法访问默认的空白图像。如果我直接在@item上使用回形针,回形针将为我返回默认图像。

如果我不想在每次调用缩略图时继续添加 if/unless 语句,那么最好的方法是什么?

I am using Paperclip to upload multiple item images. I followed some of this tutorial http://sleekd.com/general/adding-multiple-images-to-a-rails-model-with-paperclip/
My models are:

Item.rb

has_many :item_images, :dependent => :destroy
accepts_nested_attributes_for :item_images, :reject_if => lambda { |l| l['item_image'].nil? }

ItemImage.rb

class ItemImage < ActiveRecord::Base
 belongs_to :item
 belongs_to :user
 #for image. Paperclip     following Bootstrap sizes.
 has_attached_file :image,
                :styles => { :large => ["330x230>",:png], :medium => ["210x150>",:png], :small => ["90x90>",:png], :thumb => ["32x32>", :png] },
                :default_url => '/images/missing_image_:style.png',
                :storage => :s3,
                :bucket => 'iscomp',
                :s3_credentials => "#{RAILS_ROOT}/config/amazon_s3.yml",
                :path => "iscomp/:attachment/:style/:id.:extension"
 validates_attachment_presence :image
 validates_attachment_size :image, :less_than => 5.megabytes
end 

My issue is that because the images are stored on a second model, I am not able to access a default blank image when there are no images/records on the @item.item_image. If I had used paperclip directly on @item, Paperclip will return default image for me.

What's the best way of doing this if I do not want to keep adding in if/unless statements whenever I call for a thumbnail?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

高冷爸爸 2024-12-13 03:50:09

您可以通过以下方式解决此问题:

在迁移中添加 itemimage_count

  def self.up  
   add_column :item, :itemimage_count, :integer, :default => 0

 Item.reset_column_information  
   Item.all.each do |item|  
     item.update_attribute :itemimage_count, item.itemimages.length  
   end  
 end  

 def self.down  
   remove_column :item, :itemimage_count  
 end
end

这会将多个 ItemImage 与每个 Item 相关联。然后您可以在视图、控制器和模型中访问此计数器。

如果您想使用它,您可以简单地在模型中设置范围条件。

Item.rb

scope :list, :conditions => ["itemimages_count >= 0"]

Item_controller

@itemimages = Item.list

我个人会使用一组 if else 语句在您的视图中创建一个部分,每当您想要显示图像时,只需调用该部分即可。

if Item.itemimages_count == 0
    #show this image
end
 #code that displays an Item's image
    #show the image in itemimages

我希望这对某人有帮助。

You can solve this issue by:

Adding an itemimage_count in your migration

  def self.up  
   add_column :item, :itemimage_count, :integer, :default => 0

 Item.reset_column_information  
   Item.all.each do |item|  
     item.update_attribute :itemimage_count, item.itemimages.length  
   end  
 end  

 def self.down  
   remove_column :item, :itemimage_count  
 end
end

This will then associate a number of ItemImages with each Item. You can than access this counter in your views, controllers and models.

If you want to get pretty with it you can simply set a scope condition in your model.

Item.rb

scope :list, :conditions => ["itemimages_count >= 0"]

Item_controller

@itemimages = Item.list

I would personally make a partial in your views with 1 set of if else statements, whenever you want to display the images simply call out to the partial.

if Item.itemimages_count == 0
    #show this image
end
 #code that displays an Item's image
    #show the image in itemimages

I hope this helps someone.

我一直都在从未离去 2024-12-13 03:50:09

我不知道您是否正在寻找一种优雅的方法来执行此操作,但我所做的方法是创建默认图像,如果 @item.item_image 返回空白,我将显示默认的空白图像。我没有使用默认的回形针。

I don't know if you are looking for an elegant way of doing this but the way I did was to create a default image and if @item.item_image returns blank I display the default blank image. I did not use paperclip default.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文