From 31ad3f152954142c68c030db596c35e31ca93a76 Mon Sep 17 00:00:00 2001 From: Xavier Noria Date: Sat, 23 Oct 2021 21:29:40 +0200 Subject: [PATCH] Add a section about autoloading in the Getting Started Guide --- guides/source/getting_started.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/guides/source/getting_started.md b/guides/source/getting_started.md index db3fb15581..3c86e22368 100644 --- a/guides/source/getting_started.md +++ b/guides/source/getting_started.md @@ -361,6 +361,24 @@ confirming that the `root` route is also mapped to the `index` action of TIP: To learn more about routing, see [Rails Routing from the Outside In]( routing.html). +Autoloading +----------- + +Rails applications **do not** use `require` to load application code. + +You may have noticed that `ArticlesController` inherits from `ApplicationController`, but `app/controllers/articles_controller.rb` does not have anything like + +```ruby +require "application_controller" # DON'T DO THIS. +``` + +Application classes and modules are available everywhere, you do not need and **should not** load anything under `app` with `require`. This feature is called _autoloading_, and you can learn more about it in [_Autoloading and Reloading Constants_](https://guides.rubyonrails.org/autoloading_and_reloading_constants.html). + +You only need `require` calls for two use cases: + +* To load files under the `lib` directory. +* To load gem dependencies that have `require: false` in the `Gemfile`. + MVC and You -----------