Change the ActiveStorage Controller Path

自闭症网瘾萝莉.ら 提交于 2019-11-30 08:22:35

问题


Is there a way to customize the attachment urls so instead of

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

We could have something like this:

/custom_path/representations/
/custom_path/blobs/

回答1:


Recently, there was an addition which makes the route prefix configurable: https://github.com/rails/rails/commit/7dd9916c0d5e5d149bdde8cbeec42ca49cf3f6ca

Just in master branch now, but should be integrated into ~> 5.2.2 have been integrated into Rails 6.0.0 and higher.

Then, it's just a matter of configuration:

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



回答2:


Monkey patching is always on your side 😉.

Just for interest with next patch, I could change ActiveStorage Controller path:

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)

and everything seems works 🙂.




回答3:


Tested the solution by @stwienert. The config.active_storage.routes_prefix option only works for rails 6.0.0alpha and higher branch. The patch is not available for 5.2.2

I made a fork for 5.2.2 with the patch. https://github.com/StaymanHou/rails/tree/v5.2.2.1

To use it, simply replace the line gem 'rails', '~> 5.2.2' with gem 'rails', '5.2.2.1', git: 'https://github.com/StaymanHou/rails.git', tag: 'v5.2.2.1'. And run bundle install --force

coffee-rails gem will be required if you don't have it yet for rails edge install - https://github.com/rails/rails/issues/28965



来源:https://stackoverflow.com/questions/49409117/change-the-activestorage-controller-path

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!