What exactly are the "components" of Rails (ActiveRecord, ActionController, etc.)?
I just started learning Rails and Ruby. The Ruby on Rails Guide at http://guides.rubyonrails.org/getting_started.html says that Rails consists of many different "components". Some of these components, such as ActiveReco开发者_Go百科rd and ActionController, I've encountered in my Rails apps (in models and controllers, respectively).
The relevant syntax ("class Model < ActiveRecord::Base" and "class ApplicationController < ActionController::Base") make it look like these components are Ruby modules, but if they are modules in which files are they located? And how can we reference them without first using the Ruby method "require"?
UPDATE: So I found all of the built-in modules and classes. On my server, the path for the Base class of the ActiveRecord module (for example) is:
/usr/lib/ruby/gems/1.8/gems/activerecord-3.0.9/lib/active_record
But I still don't know why we can refer to these modules and classes in our models and controllers without first using Ruby's require method.
Rails components are modules which are included by default in application.rb
with require rails/all
:
- action mailer is a module for designing email service layers
- action pack is a module for handling and responding to web requests (includes action_controller, action_dispatch)
- action view is a module for handling view template lookup and rendering
- active job is a module for declaring jobs and making them run on a variety of queueing backends
- active model is non-database functionality extracted from Rails 2 Active Record (
validates :name, presence: true
) - active record connects classes to relational database tables (migrations, associations)
- active support contains all Ruby extensions (
[].blank?
)
Rails components:
- Actioncable - (Rails 5.0)
- Actionmailer
- Actionpack * Abstract controller * Action Controller * Action Dispatch * Action Pack
- Actionview
- Activejob
- Activemodel
- Activerecord
- Activestorage - (Rails 5.2)
- Activesupport
- Action Text - (Rails 6)
- ActiveRecord
- ActionPack
- ActionMailer
- ActiveModel
- ActiveResource
- ActiveSupport
Go to this page Github Rails Page and under each one, you have a detailed explanation about their purpose.
In your rails app, these are all require
d. For example if you run a rails command in your application, through script/rails command
, the script/rails
requires config/boot
which requires bundler
and then executes Bundler.setup
which requires all the gems in your Gemfile
.
精彩评论