[英]paperclip - styles lambda
我有一个脚本可以从中加载我的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
我猜我在这里忽略了一些愚蠢的事情,但任何指针我都会感激的:)
您在该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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.