Changing the ActiveStorage Controller Path

Is there a way to configure attachment urls so that instead

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

We could have something like this:

/custom_path/representations/
/custom_path/blobs/
+10
source share
4 answers

Recently, an addition has appeared that allows you to configure the route prefix: https://github.com/rails/rails/commit/7dd9916c0d5e5d149bdde8cbeec42ca49cf3f6ca

Just in the main branch now, but should be integrated in ~> 5.2.2 and higher.

Then this is just a configuration issue:

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

Sorry, I'm afraid Active Storage does not support this.

, , Active Storage , , . , Active Storage URL-, ActiveStorage::Service::DiskService#url ActiveStorage::Service::DiskService#url_for_direct_upload. ( , , .) , .

+1

😉.

, 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)

, , 🙂.

+2

Tested @stwienert solution. The option config.active_storage.routes_prefixworks only for rails 6.0.0alpha and higher. Patch not available for 5.2.2

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

To use it, simply replace the string 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 runbundle install --force

coffee-railsa gem will be needed if you do not already have one to install the edge rails - https://github.com/rails/rails/issues/28965

0
source

Source: https://habr.com/ru/post/1695161/


All Articles