Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change the ActiveStorage Controller Path

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/
like image 846
Tom Rossi Avatar asked Mar 21 '18 14:03

Tom Rossi


2 Answers

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
like image 54
stwienert Avatar answered Sep 29 '22 01:09

stwienert


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 🙂.

like image 26
ych Avatar answered Sep 29 '22 00:09

ych