ruby-on-rails-5 - 覆盖在 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
我想修改图像,而不是拥有mini
和normal
大小,我可以添加另一个大小。我也想改变路径。我该怎么做?我尝试创建一个装饰器,例如:
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
这没有做任何事情。
解决方案
不知道你现在是否已经解决了这个问题,但对于这个问题的任何未来观众......
您可以通过修改attachment_definitions
类的属性来做到这一点。例如,您可以添加一个初始化程序:
Example.attachment_definitions[:image][:styles][:normal] = "1000x500"
Example.attachment_definitions[:image][:path] = '...'
推荐阅读
- asp.net - 激活 ILogManager 时出错:没有可用的匹配绑定,并且类型不可自绑定
- grails - 将 obj 和数量列表放入 commnad obj 中
- git - 使用 Git 过滤分支时添加注释以提交
- unity3d - 通过透视相机投影正交矩阵
- google-cloud-platform - Google Cloud Scheduler - 不尊重 HTTP 标头
- logstash - 用流利的日志时间更新时间戳/时间键没有运气
- c# - 使用网络应用程序通过机器人主动发送消息
- sql - How to exclude a certain range in SQL results?
- rest - 微服务/REST - 如何在其他服务中存储对资源的引用
- python - 在扭曲脚本的 SIGTERM 上终止 docker 容器