Dynamic use of :default_url in Paperclip

Posted by dgilperez on Stack Overflow See other posts from Stack Overflow or by dgilperez
Published on 2011-10-16T12:25:14Z Indexed on 2012/06/16 21:16 UTC
Read the original article Hit count: 205

Filed under:
|
|

I'm trying to configure Paperclip to provide different missing images based on the instance's category attribute. Every category of the object has its own missing image.

This is my first take:

EDIT to add full models:

class Service < ActiveRecord::Base

  attr_accessible :logo, :logo_file_name, :logo_content_type, :logo_file_size, :logo_updated_at

  belongs_to :category, :counter_cache => true

  has_attached_file :logo,
                :path => "/:id-:style-:filename",
                :url  => ":s3_eu_url",
                :default_url => "/logos/:style/#{self.category.name]}.png",
                :styles => { :large => "600x400>",
                             :medium => "300x200>",
                             :small => "100x75>",
                             :thumb => "60x42>" }
end

class Category < ActiveRecord::Base
  attr_accessible nil

  has_many :services
end

In my view, image_tag service.logo.url(:thumb) outputs:

undefined method `category' for #<Class:0x0000010a731620>

Any ideas?

EDIT2:

A working default_url is :default_url => "/logos/:style/missing.png",

SOLUTION:

See my own answer below.

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about paperclip