Mike Rogers on a Snowmobile

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`
require "active_model/railtie"
require "active_job/railtie"
require "active_record/railtie"
# require "active_storage/engine"
require "action_controller/railtie"
require "action_mailer/railtie"
require "action_view/railtie"
require "action_cable/engine"
require "sprockets/railtie"
require "rails/test_unit/railtie"

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

Undefined method `active_storage'

If you're running into an error that looks a little bit like:

NoMethodError: undefined method `active_storage' for #<Rails::Application::Configuration

You have a reference to ActiveStorage in one of your config/application.rb or config/environments/*.rb files which looks like:

# Store uploaded files on the local file system (see config/storage.yml for options)
config.active_storage.service = :local

Remove that line and also the config/storage.yml if it is present, and you should be good to go.

Skip adding ActiveStorage on new builds

If you're starting a new project and you'd like to skip having ActiveStorage be added from the get go, add the --skip-active-storage argument when you generate a new project. For example:

rails new AwesomeProject --skip-active-storage

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