Paperclip - Default Style per Style? Possible

╄→гoц情女王★ 提交于 2019-12-12 09:44:25

问题


I'm using paperclip, and have several styles:

:styles => {:large => "300x300>", :medium => "150x150>", :small => "50x50>", :thumb => "30x30>" }

The issue is default_stype, only applies to one of the sizes... :default_style => :thumb, :default_url => url here....

How can I set default_stypes for each style type? so if I call: <%= image_tag @user.profile_pic.url(:large) %>

The LARGE style has a default_url?

Thanks


回答1:


I Would suggest to use

has_attached_file :xyz, :url  => "/assets/:id", :path => ":rails_root/assets/photos/:attachable_type/:attachable_id/:id/:basename_:style.:extension",
                  :styles => { :large => "300x300>", :medium => "150x150>", :small => "50x50>", :thumb => "30x30>"}

and to get the proper style

/assets/:id?style=:style

like localhost:3000/assets/10?style=medium

Note: attacheable_type, attachable_id are coming from the polymorphic relations..

Hope it helps...

rgds,

Kannan R




回答2:


It's fairly easy. Just create paperclip.rb in your /config/initializers and put something like this in there:

module Paperclip
  class Attachment
    def self.default_options
      @default_options ||= {
        :url               => "/system/:class/:id/:style_:filename",
        :path              => ":rails_root/public:url",
        :styles            => {},
        :processors        => [:thumbnail],
        :convert_options   => {},
        :default_url       => "/images/missing/:class_:attachment_:style.jpg",
        :default_style     => :original,
        :storage           => :filesystem,
        :whiny             => Paperclip.options[:whiny] || Paperclip.options[:whiny_thumbnails]
      }
    end
  end
end

This overrides the defaults. So you can go ahead and change :default_style to whatever you want.



来源:https://stackoverflow.com/questions/3920349/paperclip-default-style-per-style-possible

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!