Add a section about autoloading in the Getting Started Guide

This commit is contained in:
Xavier Noria 2021-10-23 21:29:40 +02:00
parent 933284aaba
commit 31ad3f1529
1 changed files with 18 additions and 0 deletions

View File

@ -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
-----------