来自Model的动态样式尺寸的回形针附件
使用Rails 2,我尝试通过Paperclip-Model中的另一个模型分离不同的动态图像大小。 我目前的方法,使用Proc,看起来如下:
class File "FileSize" has_attached_file( :attachment, :styles => Proc.new { |instance| instance.attachment_sizes } ) def attachment_sizes sizes = { :thumb => ["100x100"] } self.sizes.each do |size| sizes[:"#{size.id}"] = ["#{size.width}x#{size.height}"] end sizes end end class FileSize < ActiveRecord::Base belongs_to :file after_create :reprocess after_destroy :reprocess private def reprocess self.file.attachment.reprocess! end end
一切似乎都很好,但显然没有处理任何样式,也没有创建图像。
有没有人管理这样的事情?
– 更新 –
显然,实例上的attachment_sizes方法有时没有为#定义,但实际上不应该是#? 对我来说,这看起来像改变实例..
解决方案很简单。 在我的第一个例子中,proc是Paperclip :: Attachment的一个实例。 因为我想调用File
方法,所以必须在Proc中获取调用实例:
Proc.new { |clip| clip.instance.attachment_sizes }
instance
表示给定示例中的File
-instance。
我假设你已经掌握了回形针的所有内容,这样你就已经上传了一张图片,而现在这个过程就不起作用了。
它应该工作。 尽量不要将大小放在数组中。
你这样做
sizes = { :thumb => ["100x100"] }
但我有它,我没有把大小放在arry中
sizes = { :thumb => "100x100" }
试一试:)
- Ruby Regex – 需要替换正则表达式匹配中每个字符的出现
- 如何在Github中托管gem并使用它?
- RSpec:如何编写unit testing用例来接收在私有方法中引发的exception
- 为什么“范围”导向的行为(特别是“索引”行动)在Pundit中有不同的处理方式?
- 如何在全局的Rails中设置#encoding:utf-8
- Heroku(PG ::错误:错误:列必须出现在GROUP BY子句中或用于聚合函数
- Rails – 警告:无法批量分配受保护的属性
- 使用带有Rails carrierwave gem的ng-file-upload上传多个文件
- Rails link_to或button_to发布带参数的请求