回形针调整大小不适用于附件模型

时间:2017-12-06 16:59:47

标签: ruby-on-rails ruby ruby-on-rails-4 paperclip

我的附件模型上有两列,用户可以在其中设置要调整图像大小的尺寸。

但是,调整大小时发生的变量为nil,但在调整大小后设置为实际值。

下面是代码

  has_attached_file :file, :styles => lambda { |a|
                          { :logo => ["200x50>",:png],
                            :user_defined => ["#{a.instance.custom_width}x#{a.instance.custom_height}>",:png] }
                        }

the custom_width & custom_height are nil when conversion happens however the logo conversion works as expected.

I am using ruby 2.2.4p230 & Rails 4.2.4

以下是完整模式代码

class Attachment < ActiveRecord::Base
  belongs_to :model_one

  belongs_to :attachable, polymorphic: true
  #has_attached_file :file, styles: { logo: ['200x50>',:png] }
  has_attached_file :file, styles: lambda { |attachment| attachment.instance.styles }

  def styles
    Rails.logger.info self.inspect
    Rails.logger.info self.attachable
    styles = {}
    m = "200x50>"
    l = "#{self.custom_width}x#{self.custom_height}>"
    styles[:logo] = [m, :png]
    styles[:user_defined] = [l, :png]
    styles
  end

end

任何人都可以帮忙,如果我做错了,请告诉我吗?

0 个答案:

没有答案