更改 ActiveStorage 控制器路径

Change the ActiveStorage Controller Path

有没有办法自定义附件 url 而不是

/rails/active_storage/representations/
/rails/active_storage/blobs/

我们可以有这样的东西:

/custom_path/representations/
/custom_path/blobs/

猴子补丁永远在你身边

为了对下一个补丁感兴趣,我可以更改 ActiveStorage 控制器路径:

module MapperMonkeypatch
  def map_match(paths, options)
    paths.collect! do |path|
      path.is_a?(String) ? path.sub('/rails/active_storage/', '/custom_path/') : path
    end
    super
  end
end

ActionDispatch::Routing::Mapper.prepend(MapperMonkeypatch)

一切似乎都正常。

最近,有一个使路由前缀可配置的添加:https://github.com/rails/rails/commit/7dd9916c0d5e5d149bdde8cbeec42ca49cf3f6ca

现在在master分支,但是应该被集成到~> 5.2.2已经被集成到 Rails 6.0.0 及更高版本。

那么,就是配置问题了:

Rails.application.configure do
  config.active_storage.routes_prefix = '/whereever'
end

测试了@stwienert 的解决方案。 config.active_storage.routes_prefix 选项仅适用于 rails 6.0.0alpha 和更高版本的分支。该补丁不适用于 5.2.2

我用补丁为 5.2.2 做了一个分支。 https://github.com/StaymanHou/rails/tree/v5.2.2.1

要使用它,只需将行 gem 'rails', '~> 5.2.2' 替换为 gem 'rails', '5.2.2.1', git: 'https://github.com/StaymanHou/rails.git', tag: 'v5.2.2.1'。并且 运行 bundle install --force

coffee-rails gem 如果您还没有 rails 边缘安装,则需要 gem - https://github.com/rails/rails/issues/28965