【问题标题】:paperclip - styles lambda回形针 - 样式 lambda
【发布时间】:2012-06-21 03:34:35
【问题描述】:

我有一个脚本,我可以从中加载我的 rails 环境。 (

当我构建附件并保存其父级时,附件样式始终为 ["100>", "jpg"]

我的脚本:

require './config/environment.rb'
house = House.find(1)
house.attachments.build(doc: File.new('myfile.pdf'), category_id: 2)
house.save

型号

House < AR::Base
  has_many :attachments, :as => :attachable
end

Attachment < AR::Base
  ### has_attached_file :doc, styles: lambda {|attachment| {thumb: (attachment.instance.category_id == 2 ? ["500>", 'jpg'] : ['100>', 'jpg']} )}  
  has_attached_file :doc, styles: lambda {|attachment| {thumb: (attachment.instance.category_id == 2 ? ["500>", 'jpg'] : ['100>', 'jpg'] )}}  #category_id is always nil at this point but still still saves in the database 
  belongs_to :attachable, polymorphic: true
end

我猜我在这里忽略了一些愚蠢的事情,但我会很感激任何指针:)

【问题讨论】:

    标签: ruby-on-rails paperclip


    【解决方案1】:

    你在那个 lambda 上的语法看起来有点古怪。我不确定那个三元运算符是如何不叫“意外的':'”的。

    试一试:

    Attachment < AR::Base
      has_attached_file :doc, styles: lambda {|attachment| { thumb: (attachment.instance.category_id == 2 ? ["500>", 'jpg'] : ['100>', 'jpg'])}}
      belongs_to :attachable, polymorphic: true
    end
    

    【讨论】:

    • 这不是复制和粘贴工作,我只是在离开工作计算机时快速模拟了它。我编辑了操作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-24
    • 2015-06-13
    • 1970-01-01
    相关资源
    最近更新 更多