【问题标题】:Dynamic use of :default_url in Paperclip在 Paperclip 中动态使用 :default_url
【发布时间】:2011-10-16 12:25:14
【问题描述】:

我正在尝试配置 Paperclip 以根据实例的类别属性提供不同的缺失图像。每个类别的对象都有自己的缺失图像。

这是我的第一次拍摄:

编辑以添加完整模型:

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

在我看来,image_tag service.logo.url(:thumb) 输出:

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

有什么想法吗?

EDIT2:

一个有效的 default_url 是 :default_url => "/logos/:style/missing.png",

解决方案:

在下面查看我自己的答案。

【问题讨论】:

    标签: ruby-on-rails paperclip


    【解决方案1】:

    我在this gist 和另一个question in stackoverflow 之后找到了解决方案。

    我的工作解决方案:

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

    还有一个初始化器 paperclip_default_url_fix.rb

    module Paperclip
      module Interpolations
        def self.interpolate(pattern, *args)
          pattern = args.first.instance.send(pattern) if pattern.kind_of? Symbol
    
          all.reverse.inject(pattern.dup) do |result, tag|
            result.gsub(/:#{tag}/) do |match|
              send(tag, *args)
            end
          end
        end
      end
    end
    

    【讨论】:

    • 我知道这个话题已经很老了,但无论如何——请注意这段代码,因为它会减慢获取 A LOT 等图像的 URL 的速度,而且会消耗太多内存。
    【解决方案2】:

    现在回形针 wiki 上有一个非常干净的解决方案,适用于 :url:path:default_url,它可以解决问题。

    https://github.com/thoughtbot/paperclip/wiki/Interpolations

    【讨论】:

      【解决方案3】:

      您可以将 Proc 作为 :default_url 传递给回形针。见https://github.com/thoughtbot/paperclip/blob/master/lib/paperclip/attachment.rb#L135。 Paperclip 将使用 Attachment 对象作为参数调用该过程。 Attachment 对象有一个访问器“实例”,它是它附加到的 ActiveRecord 对象实例。在您的情况下,您应该:

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

      【讨论】:

        【解决方案4】:

        你不需要self:

        :default_url => "/logos/:style/#{category.name}.png",
        

        【讨论】:

        • 同样的问题:#<0x0000010a593e08>
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-05-12
        • 1970-01-01
        • 1970-01-01
        • 2012-03-22
        相关资源
        最近更新 更多