default_style在Rails 3.2.1上的Paperclip中不起作用

时间:2012-03-21 19:18:56

标签: ruby-on-rails paperclip

我正在尝试在我的应用程序中使用default_style,该应用程序在Rails 3.2.1上的Paperclip 2.2.5上运行。出于某种原因,即使我指定使用较小的尺寸,它仍然默认为原始尺寸。这是我定义附件的模型:

class Profile < ActiveRecord::Base
has_one :user

has_attached_file :cover_image,
                :styles => { :thumb => "x70", :super_thumb => "x28" },
                :default_style => :thumb,
                :url => "/assets/products/:id/:style/:basename.:extension",  
                :path => ":rails_root/public/assets/products/:id/:style/:basename.:extension",
                :default_url =>  "/assets/products/default/default_:style_pic.jpg"

end

当用户在不上传图像的情况下创建他/她的个人资料时,原始大小的默认图片会放在那里。问题是我希望它是:拇指大小,如上所述。有谁知道为什么这不起作用?

感谢。

1 个答案:

答案 0 :(得分:2)

没关系,我想出来了。对于那些可能有相同问题的人,将一个名为paperclip.rb的文件添加到config / initializers并将其放入:

module Paperclip
class Attachment
def default_options
  @default_options ||= {
    :default_url       => "/assets/products/default/:style/Testudo1920.jpg",
    :default_style     => :thumb,
    :storage           => :filesystem,
  }
end
end
end

然后在我附加文件的模型中,我添加了attr_accessor:

class Profile < ActiveRecord::Base
has_one :user

attr_accessor :default_url, :default_style

has_attached_file :cover_image,
                :styles => {:thumb => "x70", :header => "x50", :super_thumb => "x28"},
                :default_style => :header,
                :url => "/assets/products/:id/:style/:basename.:extension",  
                :path => ":rails_root/public/assets/products/:id/:style/:basename.:extension",
                :default_url =>  "/assets/products/default/:style/Testudo1920.jpg"

end