11

I removed ActionStorage from config/application.rb yet I see these routes when I run rake routes | less.

I have nothing set up on my /config/routes.rb...

                   Prefix Verb URI Pattern                                                                              Controller#Action
       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

Anyone know how I can remove these?

Violeta
  • 700
  • 7
  • 16
Pants
  • 2,563
  • 1
  • 20
  • 34

4 Answers4

12

Two changes are needed to remove the ActiveStorage routes from an existing Rails app:

First, in config/application.rb, comment out active_storage/engine and action_text/engine:

require_relative 'boot'

require "rails"
# Pick the frameworks you want:
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_text/engine"
require "action_cable/engine"
require "rails/test_unit/railtie"

Then, in each environment config file (e.g. config/development.rb), comment out config.active_storage.service:

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

This will remove the ActiveStorage routes:

$ rails routes
You don't have any routes defined!

Please add some routes in config/routes.rb.

For more information about routes, see the Rails guide: https://guides.rubyonrails.org/routing.html.
Josh L
  • 121
  • 1
  • 4
  • in rails 6, I also need to comment the `require 'action_mailbox/engine'` to remove these routes. – cyrilchampier Jul 16 '20 at 07:15
  • 4
    can't understand, why rails inject these routes by default. someone might want to upload files, without these routes. There should be a way, to remove routes without removing all the engine. – Ahmed Kooli Nov 17 '21 at 20:00
7

Set the draw_routes config to false on your application.rb config file https://github.com/rails/rails/blob/439d4995c1dab475b576fcb19ea95ae37e0ed222/guides/source/configuring.md#configuring-active-storage

config.active_storage.draw_routes = false

EDIT: as of now, the current stable release (6.0-stable) does not support this option, it's on the master branch and should work on the next release

arieljuod
  • 15,460
  • 2
  • 25
  • 36
  • As I understand now, that config option is not on the stable release, it's on the master branch and it will work on next releases, I don't think you can skip those routes as of now without hacking the code (which I wouldn't recommend). – arieljuod Sep 16 '19 at 14:50
  • This is the best answer as it allows you to keep using ActiveStorage but remove the routes when you don't need them. Other answers make ActiveStorage non-functional completely. – senya Jun 30 '23 at 15:31
5

There is an "official" way to do this, which is built-in into Rails. If you create new project with

$ rails new dummy_project --skip-active-storage

you can find in the newly created config/application.rb exactly what you need (Rails 6.0.1 but works since 5.2):

require "rails"
# Pick the frameworks you want:
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_mailbox/engine"
# require "action_text/engine"
require "action_view/railtie"
require "action_cable/engine"
require "sprockets/railtie"
require "rails/test_unit/railtie"

Replace require 'rails/all' with the above snippet and you should be good to go.

silverdr
  • 1,978
  • 2
  • 22
  • 27
  • 2
    With an existing project, it might be worth remove the items in the schema + any existing active record migrations – BenKoshy Mar 04 '20 at 03:06
0

This is kinda dumb, but for some reason deleting the config/storage.yml also got rid of those routes.

Pants
  • 2,563
  • 1
  • 20
  • 34
  • Well - I don't have `config/storage.yml` (application migrated to Rails 5.3 from 5.0) file but the unwanted routes show up anyway. – silverdr Dec 12 '19 at 13:44