【问题标题】:Many default routes in rails 6?rails 6中有许多默认路线?
【发布时间】:2019-09-16 02:29:28
【问题描述】:

如何删除 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 条路线(索引页)。

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-6


    【解决方案1】:

    您可以通过扩展 application.rb 文件中需要rails/all 的行,从现有应用程序中完全删除actionmailbox 功能(如果您正在升级rails 或已经运行rails new)。此文件仅包含默认的 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 ...
    

    【讨论】:

      【解决方案2】:

      我也刚刚打了这个。如果您将此代码添加到您的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 的输出。

      【讨论】:

      • 有趣的发现。谢谢!
      • 哇!这工作完美
      【解决方案3】:

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

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

      在那里你可以看到新的 Rails 应用程序选项的完整列表

      rails new --help
      

      顺便说一句:新的 Rails 应用默认不包含任何路由。见Rails routing guide

      【讨论】:

      • 注意action-mailer不会创建任何路由,问题上的所有路由都来自活动存储和action邮箱
      【解决方案4】:

      我想还没有办法做到这一点

      对于 ActiveStorage 路由,您的 config/application.rb 会有此配置

      config.active_storage.draw_routes = false
      

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

      【讨论】:

      • 谢谢,这应该会有所帮助
      • 是的,这有帮助,但仍需要删除其他的。干得好!
      【解决方案5】:

      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中删除任何与邮件相关的配置

      【讨论】:

      • 不工作,至少在我的电脑上
      猜你喜欢
      • 2015-09-08
      • 2014-08-03
      • 1970-01-01
      • 1970-01-01
      • 2020-11-16
      • 1970-01-01
      • 1970-01-01
      • 2015-08-30
      • 1970-01-01
      相关资源
      最近更新 更多