rails 6 中的许多默认路由?
Many default routes in rails 6?
如何删除 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 条路线(索引页)。
我想还没有办法做到这一点
对于 ActiveStorage 路由,您的 config/application.rb
将具有此配置
config.active_storage.draw_routes = false
- https://github.com/rails/rails/blob/c6ef71ccf23fcfb73c3cce82d8437a9bcb5bd07c/railties/test/application/configuration_test.rb#L2608, 不在当前稳定版本中,但在下一个版本的master分支中
对于 ActionMailbox 路由,我什至在 master 分支上也找不到任何东西。我想下一个版本会有类似 active_storage 配置的东西。
如果您不会在您的项目中使用这些功能,您应该 运行
rails new blog --skip-active-storage --skip-action-mailer --skip-action-mailbox
在那里你可以看到新的 Rails 应用程序选项的完整列表
rails new --help
顺便说一句:新 Rails 应用默认不包含任何路由。参见 Rails routing guide
我也刚打了这个。如果您将此代码添加到您的 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
.[=13= 的输出]
您可以通过展开您的 application.rb 文件需要 rails/all
。此文件仅包含默认 rails
库。 Have a look。
通过替换此行,您不仅可以排除路由,还可以防止您的应用加载从未使用过的代码。
这是一个示例,显示了我的 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 ...
在 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
中删除任何与邮件相关的配置
如何删除 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 条路线(索引页)。
我想还没有办法做到这一点
对于 ActiveStorage 路由,您的 config/application.rb
将具有此配置config.active_storage.draw_routes = false
- https://github.com/rails/rails/blob/c6ef71ccf23fcfb73c3cce82d8437a9bcb5bd07c/railties/test/application/configuration_test.rb#L2608, 不在当前稳定版本中,但在下一个版本的master分支中
对于 ActionMailbox 路由,我什至在 master 分支上也找不到任何东西。我想下一个版本会有类似 active_storage 配置的东西。
如果您不会在您的项目中使用这些功能,您应该 运行
rails new blog --skip-active-storage --skip-action-mailer --skip-action-mailbox
在那里你可以看到新的 Rails 应用程序选项的完整列表
rails new --help
顺便说一句:新 Rails 应用默认不包含任何路由。参见 Rails routing guide
我也刚打了这个。如果您将此代码添加到您的 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
.[=13= 的输出]
您可以通过展开您的 application.rb 文件需要 rails/all
。此文件仅包含默认 rails
库。 Have a look。
通过替换此行,您不仅可以排除路由,还可以防止您的应用加载从未使用过的代码。
这是一个示例,显示了我的 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 ...
在 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