Mike Rogers on a Snowmobile

Mike Rogers

Web Developer

Removing Active Storage Routes from Rails 5.2

Posted on

Rails 5.2 has landed and it brings some pretty cool new additions to the Rails stack.

However while upgrading one of my apps, I ran rails routes and noticed some new Active Storage routes had appeared:

       rails_service_blob GET  /rails/active_storage/blobs/:signed_id/*filename(.:format)                               active_storage/blobs#show
rails_blob_representation GET  /rails/active_storage/representations/:signed_blob_id/:variation_key/*filename(.:format) active_storage/representations#show
       rails_disk_service GET  /rails/active_storage/disk/:encoded_key/*filename(.:format)                              active_storage/disk#show
update_rails_disk_service PUT  /rails/active_storage/disk/:encoded_token(.:format)                                      active_storage/disk#update
     rails_direct_uploads POST /rails/active_storage/direct_uploads(.:format)                                           active_storage/direct_uploads#create

As I didn't intend to have any uploaded content in this app, I wanted to remove them. The documentation unfortunately doesn't mention much about these new routes. So I got digging through the rails core code & found a solution.

How to remove these routes

In your config/application.rb file, find the line:

require 'rails/all'

And replace it with:

require "rails"

# Include each railties manually, excluding `active_storage/engine`
%w(
  active_record/railtie
  action_controller/railtie
  action_view/railtie
  action_mailer/railtie
  active_job/railtie
  action_cable/engine
  rails/test_unit/railtie
  sprockets/railtie
).each do |railtie|
  begin
    require railtie
  rescue LoadError
  end
end

What this will do is instead of requiring rails/all, it'll just require the railties we're planning to use.

By removing active_storage/engine, the Active Storage railties is no longer required and the routes it requires are no longer mounted. 👍

This was written by Mike Rogers, a freelance Ruby on Rails developer based in London.

Share the ♥ by sharing this!

If you want to discuss this post, feel free to tweet me (@MikeRogers0) or drop me an email. Any code samples unless stated otherwise are licensed under the The MIT License (MIT). Spotted a mistake? Send me a pull request :)