Reputation: 13378
Using Rails 3.2 and Paperclip 3.4.2. I have the following:
# photo.rb
has_attached_file :data,
:styles => {
:picture_lightbox => ["600x450>", :jpg],
:picture_preview => ["250x250^", :jpg],
:picture_thumb => ["76x76^", :jpg]
},
:default_url => "placeholder_:style.png"
# shop.rb
has_many :photos
# show.html.erb
<% if !shop.photos.blank? %>
<%= image_tag(shop.photos[0].data.url(:picture_thumb)) %>
<% else %>
<%= image_tag('placeholder_picture_thumb.png') %>
<% end %>
While this works, but it defeats the purpose of specifying :default_url
in photo.rb
, because I don't know a way to show the default image when shop.photos
(which is an array of photo objects) is blank.
This is not about asset pipeline. It's about how can I detect that shop.photos
is blank, then it returns the default image url, instead of explicitly specifying the default image url. What should I change?
Upvotes: 2
Views: 2551
Reputation: 1671
The purpose of :default_url
on Paperclip in your case is to set a default url for photo
object. But you have a problem with showing a default "cover photo" of shop
. This is additional logic in your code. You cannot achieve this only with Paperclip's :default_url
option. If you want to take advantage of :default_url
option, I will suggest you to create a method in shop.rb
which looks something like this:
def cover_url
# I guess you want to use first photo based on your code
photos.first_or_initialize.data.url(:picture_thumb)
end
Then in your view you will have just <%= image_tag(shop.cover_url) %>
Upvotes: 2
Reputation: 5111
Actually the default URL will work when you have a relation like
class User
has_attached_file :photo
end
when user.photo is nil, default user.photo.url will return default URL.
What you have done in your case seems correct to me.
Upvotes: -1