【发布时间】:2020-01-05 18:07:56
【问题描述】:
我正在使用 Rails 5.2 和 ActiveStorage 5.2.3 和 DirectDiskService。
为了让用户上传在目录中很好地分组,并且为了能够随意使用 CDN(例如 CloudFlare 或 CloudFront 或任何其他),我试图在 ApplicationController 中设置一个方法来设置(本地) 上传路径,例如:
class ApplicationController < ActionController::Base
before_action :set_uploads_path
# ...
private
def set_upload_paths
# this doesn't work
ActiveStorage::Service::DirectDiskService.set_root p: "users/#{current_user.username}"
# ... expecting the new root to become "public/users/yaddayadda"
end
end
在 config/initializers/active_storage.rb 我有:
module SetDirectDiskServiceRoot
def set_root(p:)
@root = Rails.root.join("public", p)
@public_root = p
@public_root.prepend('/') unless @public_root.starts_with?('/')
end
end
ActiveStorage::Service::DirectDiskService.module_eval { attr_writer :root }
ActiveStorage::Service::DirectDiskService.class_eval { include SetDirectDiskServiceRoot }
它确实让我可以在服务的新实例上设置根目录,但不能在 Rails 应用程序正在使用的实例上设置根目录。
我做错了什么?或者我该如何完成?
【问题讨论】:
标签: ruby-on-rails rails-activestorage