在回形针上使用多个嵌套图像时的默认图片
我正在使用回形针上传多个项目图像。我遵循了本教程的一些内容 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以通过以下方式解决此问题:
在迁移中添加 itemimage_count
这会将多个 ItemImage 与每个 Item 相关联。然后您可以在视图、控制器和模型中访问此计数器。
如果您想使用它,您可以简单地在模型中设置范围条件。
Item.rb
Item_controller
我个人会使用一组 if else 语句在您的视图中创建一个部分,每当您想要显示图像时,只需调用该部分即可。
我希望这对某人有帮助。
You can solve this issue by:
Adding an itemimage_count in your migration
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
Item_controller
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.
I hope this helps someone.
我不知道您是否正在寻找一种优雅的方法来执行此操作,但我所做的方法是创建默认图像,如果 @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.