Ahmed El Bannan
Ahmed El Bannan

Reputation: 83

Many default routes in rails 6?

How do I remove the default routes in Rails 6?

I just installed Rails 6.0.0 and ran 'rails new blog'. I went to take a look at the routes and found a ton of routes (see below). I've tried creating several new projects and they all have the same default routes.

$ rake routes

Prefix Verb   URI Pattern                                                                          Controller#Action
        rails_mandrill_inbound_emails POST   /rails/action_mailbox/mandrill/inbound_emails(.:format)                                  action_mailbox/ingresses/mandrill/inbound_emails#create
        rails_postmark_inbound_emails POST   /rails/action_mailbox/postmark/inbound_emails(.:format)                                  action_mailbox/ingresses/postmark/inbound_emails#create
           rails_relay_inbound_emails POST   /rails/action_mailbox/relay/inbound_emails(.:format)                                     action_mailbox/ingresses/relay/inbound_emails#create
        rails_sendgrid_inbound_emails POST   /rails/action_mailbox/sendgrid/inbound_emails(.:format)                                  action_mailbox/ingresses/sendgrid/inbound_emails#create
         rails_mailgun_inbound_emails POST   /rails/action_mailbox/mailgun/inbound_emails/mime(.:format)                              action_mailbox/ingresses/mailgun/inbound_emails#create
       rails_conductor_inbound_emails GET    /rails/conductor/action_mailbox/inbound_emails(.:format)                                 rails/conductor/action_mailbox/inbound_emails#index
                                      POST   /rails/conductor/action_mailbox/inbound_emails(.:format)                                 rails/conductor/action_mailbox/inbound_emails#create
    new_rails_conductor_inbound_email GET    /rails/conductor/action_mailbox/inbound_emails/new(.:format)                             rails/conductor/action_mailbox/inbound_emails#new
   edit_rails_conductor_inbound_email GET    /rails/conductor/action_mailbox/inbound_emails/:id/edit(.:format)                        rails/conductor/action_mailbox/inbound_emails#edit
        rails_conductor_inbound_email GET    /rails/conductor/action_mailbox/inbound_emails/:id(.:format)                             rails/conductor/action_mailbox/inbound_emails#show
                                      PATCH  /rails/conductor/action_mailbox/inbound_emails/:id(.:format)                             rails/conductor/action_mailbox/inbound_emails#update
                                      PUT    /rails/conductor/action_mailbox/inbound_emails/:id(.:format)                             rails/conductor/action_mailbox/inbound_emails#update
                                      DELETE /rails/conductor/action_mailbox/inbound_emails/:id(.:format)                             rails/conductor/action_mailbox/inbound_emails#destroy
rails_conductor_inbound_email_reroute POST   /rails/conductor/action_mailbox/:inbound_email_id/reroute(.:format)                      rails/conductor/action_mailbox/reroutes#create
                   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

I was expecting at most 1 route (index page).

Upvotes: 8

Views: 4608

Answers (5)

Ahmed Kooli
Ahmed Kooli

Reputation: 763

In config/application.rb replace this line:

require "rails/all"

With its content and keep only the rail ties you need:

require "rails"

%w(
  active_record/railtie
  active_storage/engine
  action_controller/railtie
  action_view/railtie
  action_mailer/railtie  <-- REMOVE THIS!
  active_job/railtie
  action_cable/engine
  action_mailbox/engine  <-- REMOVE THIS!
  action_text/engine
  rails/test_unit/railtie
  sprockets/railtie
).each do |railtie|
  begin
    require railtie
  rescue LoadError
  end
end

Then remove any mail related config from config/environments/*.rb

Upvotes: 0

Graeme
Graeme

Reputation: 990

You can remove the actionmailbox functionality completely from an existing app (if you are upgrading rails or if you already ran rails new) by expanding the line in your application.rb file that requires rails/all. This file just includes the default rails libraries. Have a look.

By replacing this line, you'll not only exclude the routes but also prevent your app from loading code that never gets used.

Here's an example, showing the top portion of my application.rb file:

# config/application.rb

require_relative 'boot'

# Check out what rails/all.rb is currently expanded to:
#  https://github.com/rails/rails/blob/master/railties/lib/rails/all.rb
# Replace `require 'rails/all'` with just the libs that you want and 
# exclude the rest
require 'active_record/railtie'
# require 'active_storage/engine'
require 'action_controller/railtie'
require 'action_view/railtie'
require 'action_mailer/railtie'
require 'active_job/railtie'
require 'action_cable/engine'
# require 'action_mailbox/engine'
require 'action_text/engine'
require 'rails/test_unit/railtie'
require 'sprockets/railtie'

# the rest of your initialization follows here ...

Upvotes: 6

davetron5000
davetron5000

Reputation: 24891

I just hit this as well. If you add this code to your config/application.rb, it will remove those routes:

require_relative 'boot'

require 'rails/all'

# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)

module YourApp
  class Application < Rails::Application
    # Initialize configuration defaults for originally generated Rails version.
    config.load_defaults 6.0

    # CODE YOU SHOULD ADD vvvvvv
    initializer(:remove_action_mailbox_and_activestorage_routes, after: :add_routing_paths) { |app|
      app.routes_reloader.paths.delete_if {|path| path =~ /activestorage/}
      app.routes_reloader.paths.delete_if {|path| path =~ /actionmailbox/ }
    }
    # CODE YOU SHOULD ADD ^^^^^^^^

    # Settings in config/environments/* take precedence over those specified here.
    # Application configuration can go into files in config/initializers
    # -- all .rb files in that directory are automatically loaded after loading
    # the framework and any gems in your application.
  end
end

From what I can tell, this uses a private API to access the files that will create these routes and removes them from the path. I have no idea if this will remove them from production or not, and since it's a private API it'll likely break later, but at least it cleans up the output of bin/rails routes.

Upvotes: 8

Lyzard Kyng
Lyzard Kyng

Reputation: 1568

If you won't use these features in your project you should run

rails new blog --skip-active-storage --skip-action-mailer --skip-action-mailbox

There you can see full list of new Rails app options

rails new --help

By the way: new Rails app doesn't contain any routes by default. See Rails routing guide

Upvotes: 3

arieljuod
arieljuod

Reputation: 15848

I guess there's no way to do that yet

For the ActiveStorage routes you'll have this config for your config/application.rb

config.active_storage.draw_routes = false

For ActionMailbox routes I can't find anything even on the master branch. I guess there will be something like the active_storage config for the next release.

Upvotes: 3

Related Questions