首页 > 解决方案 > 覆盖在 gem 中设置的回形针 has_attached_file 设置

问题描述

在我的项目中,我有一个使用回形针作为文件附件的 gem。gem中的示例模型:

class Example do
...
    has_attached_file :image,
      styles: { mini: '32x32>', normal: '128x128>' },
      default_style: :mini,
      url: '/example/url/:id/:style/:basename.:extension',
      path: ':rails_root/public/example/url/:id/:style/:basename.:extension',
...
end

我想修改图像,而不是拥有mininormal大小,我可以添加另一个大小。我也想改变路径。我该怎么做?我尝试创建一个装饰器,例如:

Example.class_eval do
    has_attached_file :image,
      styles: { mini: '32x32>', normal: '128x128>', large: '1024x1024' },
      default_style: :mini,
      url: '/example/url/:id/:style/:basename.:extension',
      path: 'updated/example/url/:id/:style/:basename.:extension',
end

这没有做任何事情。

标签: ruby-on-rails-5paperclip

解决方案


不知道你现在是否已经解决了这个问题,但对于这个问题的任何未来观众......

您可以通过修改attachment_definitions类的属性来做到这一点。例如,您可以添加一个初始化程序:

Example.attachment_definitions[:image][:styles][:normal] = "1000x500"
Example.attachment_definitions[:image][:path] = '...'

推荐阅读