Rails 6中有许多默认路由吗?

8

如何移除Rails 6中的默认路由?

我刚安装了Rails 6.0.0并运行'rails new blog'。我查看了路由并发现有大量的路由(见下文)。我尝试创建几个新项目,它们所有的默认路由都相同。

$ 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

我原本预计最多只有1个路由(主页)。
5个回答

7

我也碰到了这个问题。如果您将以下代码添加到config/application.rb中,它将删除这些路由:

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

据我所知,这使用私有API来访问创建这些路由的文件,并将它们从路径中删除。我不知道这是否会将它们从生产环境中删除,而且由于它是私有API,很可能会在以后出现问题,但至少它可以清理bin/rails routes的输出结果。

有趣的发现。谢谢! - Ahmed El Bannan
哇!这个完美地工作了。 - Erick

6

您可以通过在应用程序.rb文件中扩展需要 rails/all 的行,来完全删除现有应用程序中的 actionmailbox 功能(如果您正在升级rails或者你已经运行了rails new)。该文件仅包含默认的rails库。 请查看

通过替换这一行,您不仅将排除路由,还将防止应用程序加载从未使用过的代码。

以下是示例,显示我的application.rb文件的顶部部分:

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

3
我想目前还没有办法做到这一点。
对于ActiveStorage路由,您需要在config/application.rb中配置以下内容。
config.active_storage.draw_routes = false
  • 这是链接,它不在当前稳定版本中,但它在下一个版本的主分支上。

对于ActionMailbox路由,即使在主分支上我也找不到任何东西。我猜下一个版本会有类似active_storage配置的内容。


谢谢,这应该会有所帮助。 - Ahmed El Bannan
是的,这有所帮助,但仍需要移除其他内容。做得很好! - Erick

3

如果您在项目中不会使用这些功能,您应该运行

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

您可以在那里查看新的Rails应用程序选项的完整列表。

rails new --help

顺便提一下:新的Rails应用程序默认不包含任何路由。请参阅Rails路由指南


1
请注意,Action Mailer 不会创建任何路由,问题中的所有路由都来自 Active Storage 和 Action Mailbox。 - arieljuod

0
config/application.rb 中替换这一行:
require "rails/all"

根据需要保留其内容并仅保留所需的铁轨连接器:

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

然后从config/environments/*.rb中删除任何与邮件相关的配置


无法工作,在我的电脑上至少是这样。 - Erick

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接