简体   繁体   English

Rails App中的application.rb文件的说明

[英]explanation of application.rb file in Rails App

I'm trying to get a deeper understanding of how a rails app initializes. 我正在努力深入了解rails应用程序的初始化方式。 I'm looking over the config/application.rb file and I'm confused by these three lines: 我正在查看config / application.rb文件,我对这三行感到困惑:

require_relative 'boot'

require 'rails/all'

Bundler.require(*Rails.groups)

From what I can tell all three of these lines are loading the gems used by the Rails Application. 据我所知,所有这三行都加载了Rails应用程序使用的gem。 boot.rb appears to load all the gems as does Bundler.require(*Rails.groups) . boot.rb似乎像Bundler.require(* Rails.groups)一样加载所有的gem。 Why is it necessary to have all three lines of code? 为什么有必要使用所有三行代码?

Mostly correct, and you can verify what is "needed" by disabling one line at a time in a working Rails app. 大多数情况下都是正确的,您可以通过在工作的Rails应用程序中一次禁用一行来验证“需要”的内容。

  • require_relative 'boot' : Application still runs. require_relative 'boot' :应用程序仍然运行。
  • require 'rails/all' : Method not found error for a gem not listed in my Gemfile (one of Rails' built-ins (require 'rails/all') require'trail require 'rails/all' :找不到我的Gemfile中列出的gem的方法错误( Rails的内置插件之一 (需要'rails / all')
  • Bundler.require(*Rails.groups) : Method not found for gem from Gemfile. Bundler.require(*Rails.groups) :从Gemfile找不到gem的方法。

So, the second and third are independent and essential. 所以,第二和第三是独立和必要的。 boot.rb's call to bundler/setup cannot stand in for either of the other two, because its function is actually to clean the load path by making sure that only Gemfile gems are included, and everything else is removed. boot.rb对bundler / setup的调用不能代替其他两个,因为它的功能实际上是通过确保只包含Gemfile gem并清除其他所有内容来清理加载路径 See the last line of the Bundler setup source . 请参阅Bundler安装源的最后一行。 So, while the app runs, it could be running with access to other gems that you did not intend to include, and give you a false sense that the app is working when it could fail for another user who only installed the Gemfile dependencies . 因此,当应用程序运行时,它可以运行时可以访问您不打算包含的其他gem,并且如果应用程序在仅安装了Gemfile依赖项的另一个用户可能会失败,会误认为该应用程序正在运行。

So you may get away with only the second and third in the short term, but would definitely want all three on anything that someone else may someday have to execute. 因此,你可能在短期内只能获得第二和第三,但绝对希望所有这三个都能在其他人可能有一天必须执行的任何事情上。 The overhead is minimal so I would not remove any of these. 开销很小,所以我不会删除任何这些。

require_relative 'boot'

Sets up Bundler and load paths for gems 为gem设置Bundler和加载路径

require 'rails/all'

This loads the rails gems. 这会加载导轨宝石。 It can be replaced in order to explicitly require only the rails gems that you need (ie require "action_mailer/railtie " 它可以被替换,以明确只需要你需要的rails gems(即require "action_mailer/railtie

Bundler.require(*Rails.groups)

This requires gems listed in your Gemfile by default. 这需要默认情况下Gemfile中列出的gem。 If you remove this line, you would have to require each gem by hand. 如果删除此行,则必须手动要求每个宝石。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM