2

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?

Victor
  • 13,010
  • 18
  • 83
  • 146
  • Please, Check this link http://stackoverflow.com/questions/9646549/default-url-in-paperclip-broke-with-asset-pipeline-upgrade – Bachan Smruty Sep 05 '13 at 08:38
  • @BachanSmruty Thanks, but it's not about asset pipeline. It's about detecting `shop.photos.blank?`, then use the `default` image. – Victor Sep 05 '13 at 08:40
  • Bad, but working solution: to build new shop photo if shop.photos is blank. – hedgesky Sep 05 '13 at 08:57

2 Answers2

2

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) %>

ventsislaf
  • 1,651
  • 12
  • 21
-1

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.

techvineet
  • 5,041
  • 2
  • 30
  • 28