ruby on rails - default image for paperclip is not displayed -
while looping through objects in view trying display first attached image of listing this:
<%= image_tag listing.assets.first.attachment.url(:small) %>
this works fine if listings have @ least 1 image if 1 listing doesn't have image, default image not shown. instead undefined method `attachment' nil:nilclass.
which logical because object nil have in model
has_attached_file :attachment, :styles => { :medium => "300x300>", :small => "200x200>", :thumb => "100x100>" }, :default_url => "no_image_:style.jpg"
so assume if image_tag doesn't receive ai url should display "no_image_small.jpg" in app/assets/images folder.
is how should work? how can display no_image_small.jpg if image doesn't exist , nill?
im guessing have listing model looks this:
class listing < activerecord::base has_many :assets ... end
and asset class this
class asset < activerecord::base has_attached_file :attachment, :styles => { :medium => "300x300>", :small => "200x200>", :thumb => "100x100>" }, :default_url => "no_image_:style.jpg" ... end
the problem not paperclip or setup, listing don't have associated assets.
edit:
here solution still default image, association:
<% asset = listing.assets.first || listing.assets.build %> <%= image_tag asset.attachment.url(:small) %>
Comments
Post a Comment