Rails 应用程序的生命周期 [英] Rails applications' life cycle

查看:47
本文介绍了Rails 应用程序的生命周期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试了解 Rails 应用程序的生命周期.application_controller.rb 什么时候运行?是每次更改还是每次请求都只执行一次?

I'm trying to learn the life cycle of a rails application. When is application_controller.rb run? Is it just once every time it's changed, or on every request?

我想了解以下文件的相同信息:

I want to know the same about the following file:

  • config/environments/*.rb(开发、生产或测试,取决于当前模式)
  • boot.rb
  • 环境.rb
  • routes.rb

我问这个的原因之一是,我想知道放在哪里

One of the reasons I'm asking this is, I want to know where is a good place to put

  • 初始化代码
  • 自定义配置数据

@Gdeglin 的回答很好,但我实际上很想知道每个文件何时运行.

@Gdeglin's answer is good, but I'm actually interested in knowing when each of these files run.

推荐答案

application_controller.rb

ApplicationController 是所有控制器的父类.出于这个原因,其中声明的方法将可供所有控制器使用.

application_controller.rb

ApplicationController is a parent class to all controllers. Methods declared in it will be available to all controllers for this reason.

ApplicationController 是一个方便的地方,用于过滤您希望应用于应用程序中所有控制器的过滤器,或者您希望提供给所有控制器的方法.

ApplicationController is a convenient place to filters that you want to apply to all controllers in your application, or methods that you wish to make available to all of them.

config/environments/*.rb 中的文件会覆盖默认 config/enviornment.rb 文件中的设置,具体取决于您的服务器运行的环境(开发/生产).一个例子是在开发中错误被打印到屏幕上,在生产中返回一个通用错误页面.这个设置在 config/environments/development.rb

The files in config/environments/*.rb override settings in the default config/enviornment.rb file depending on what environment your server is running in (development/production). One example is that in development errors are printed to the screen and in production a generic error page is returned. This setting is in config/environments/development.rb

boot.rb 用作 rails 初始化过程的一部分.您通常不需要,也可能不应该触摸它.

boot.rb is used as part of the rails initialization process. You usually don't need to, and likely shouldn't touch it.

environment.rb 是您的应用程序的通用配置文件.

environment.rb is the generic configuration file for your application.

routes.rb 用于定义您的应用程序如何处理对特定 url 的请求.例如,您可能希望将所有 404 请求转到特定操作,而不是由默认错误处理程序处理:

routes.rb is used to define how your application handles requests to specific urls. For example, you may want to have all 404 requests go to a specific action instead of being handled by the default error handler:

map.connect '*path', :controller => 'home', :action => 'on_404'

这也是实现 RESTful 应用程序的重要部分.

It is also an important part of implementing a RESTful application.

初始化代码和自定义配置数据都应该放在environment.rb中(阅读这个文件中的注释).如果您希望某些代码仅在开发中或仅在生产中在初始化期间运行,请将其分别放置在 config/environments/development.rb 或 config/environments/production.rb 中.

Both initialization code and custom configuration data should be placed in enviornment.rb (read the comments in this file). If you want certain code to run during initialization only in development or only in production, place it in config/environments/development.rb or config/environments/production.rb respectively.

此处提供了有关这些文件在初始化期间何时运行的良好概述:

A good overview on when each of these files is run during initialization is available here:

http://toolmantim.com/articles/environments_and_the_rails_initialisation_processhttps://github.com/toolmantim/toolmantim/blob/master/文章/environments_and_the_rails_initialisation_process.haml

基本上步骤是:

  1. Rails 初始化器已加载(http://api.rubyonrails.org/classes/Rails/Initializer.html)

rails Initializer 设置日志记录,然后加载 environment.rb

The rails Initializer sets up logging and then loads environment.rb

environment.rb 加载 boot.rb

environment.rb loads boot.rb

boot.rb 设置 RAILS_ROOT 常量并将 Rails 库和应用程序代码添加到 LOAD_PATH

boot.rb sets the RAILS_ROOT constant and adds rails libraries and application code to the LOAD_PATH

environment.rb 执行 Rails::Initializer.run.

environment.rb executes Rails::Initializer.run.

rails 框架已加载(ActiveRecord、ActionMailer 等)

The rails framework is loaded (ActiveRecord, ActionMailer, etc.)

您环境的特定配置文件已加载 (config/environments/development.rb.)

Your environment's specific config file is loaded (config/environments/development.rb.)

after_initializeto_prepare 如果你已经创建了回调,就会执行

after_initialize and to_prepare callbacks are executed if you have created any

Rails 已完成加载并准备好处理请求

Rails has finished loading and is ready to handle requests

这篇关于Rails 应用程序的生命周期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆