---
title: "beast学习笔记——4,enviroment.rb"
author: "helloqidi"
date: "2010-12-22T16:37:56Z"
tags: []
categories: ["编程语言"]
slug: "beast-4-enviroment-rb"
draft: false
lastmod: "2010-12-22T16:37:56Z"
id: 849488
url: "https://helloqidi.iteye.com/blog/849488"
---

**beast学习笔记——4,enviroment.rb**

参考：

[http://my.oschina.net/fireflyman/blog/4255](http://my.oschina.net/fireflyman/blog/4255)

[http://ryandaigle.com/articles/2008/4/1/what-s-new-in-edge-rails-gem-dependencies](http://ryandaigle.com/articles/2008/4/1/what-s-new-in-edge-rails-gem-dependencies)

 

总：每当你启动一个进程(例如Webrick服务器)处理Rails请求的时候,第一件发生的事情就是加载config/enviroment.rb

 

1,

(1)代码

\# Be sure to restart your server when you modify this file

（2）表示

每次修改enviroment.rb，一定要重新你启动服务

 

2，

（1）代码

\# Uncomment below to force Rails into production mode when  
\# you don't control web/app server and can't set it the proper way  
\# ENV\['RAILS\_ENV'] ||= 'production'

 

（2）表示

如果你将RAILS\_ENV设置为生产模式,或者修改常量RAILS\_ENV,它将使Rails程序中的一切运行于生产模式.  
例如,test\_helper.rb,我们可以看到它在加载环境配置之前是先把RAILS\_ENV设置为测试模式的,因此它将不能正常工作. ENV\["RAILS\_ENV"] = "test"

require File.expand\_path(File.dirname(\_\_FILE\_\_) + "/../config/environment")

require 'test\_help'

但是，如果想把数据表也迁移到production中，需要使用命令：

rake db:migrate RAILS\_ENV=product

3，

（1）代码

\# Specifies gem version of Rails to use when vendor/rails is not present  
RAILS\_GEM\_VERSION = '2.3.3' unless defined? RAILS\_GEM\_VERSION

（2）表示

该设置告诉environment.rb应该加载哪个版本的Rails.(备注:一旦脚本确认了加载哪个版本的Rails,它将加载对应的Rails　Gem.)

 

4

（1）代码

\# Bootstrap the Rails environment, frameworks, and default configuration  
require File.join(File.dirname(\_\_FILE\_\_), 'boot')

（2）表示

enviroment.rb之后的这一行,在加载config/boot.rb后,才真正启动了Rails，boot.rb启动脚本是Rails应用程序生成的一部分,但不能修改.它能够协助你检查Rails的安装是否有问题

 

5

Rails::Initializer.run do |config|  
  # Settings in config/environments/* take precedence over those specified here.  
  # Application configuration should go into files in config/initializers  
  # -- all .rb files in that directory are automatically loaded.  
  # See Rails::Configuration for more options.

 

#当应用被加载时，rails会自动在系统中（应该是C:\\Ruby187\\lib\\ruby\\gems\\1.8\\gems目录下）查找并require下面这些gem

#注意，config/environments/目录下的文件中的cofig.gem，优先级大于enviroment.rb中的

#如果你系统中没有，可以通过 rake gems:install来安装，或者通过gem install TheGemName一个个安装到系统中  
#需要一个来自**非标准**站点的gem，lib名称为will\_paginate（默认是mislav-will\_paginate）  
  config.gem 'mislav-will\_paginate', :lib =&gt; "will\_paginate",  
    :source =&gt; "[http://gems.github.com](http://gems.github.com)"

 

#需要最新版本的bluecloth  
  config.gem "bluecloth"

  config.gem 'RedCloth', :lib =&gt; "redcloth",  
    :source =&gt; "http://code.whytheluckystiff.net"

 

#跳过不想使用的Rails框架  
  # Skip frameworks you're not going to use (only works if using vendor/rails).  
  # To use Rails without a database, you must remove the Active Record framework  
  # config.frameworks -= \[ :active\_record, :active\_resource, :action\_mailer ]

 

#指定加载的plugins及顺序，默认是全部plugins。plugins所在目录为/vendor/plugins  
  # Only load the plugins named here, in the order given. By default, all plugins  
  # in vendor/plugins are loaded in alphabetical order.  
  # :all can be used as a placeholder for all plugins not explicitly named  
  # config.plugins = \[ :exception\_notification, :ssl\_requirement, :all ]

#添加另外的加载路径#备注:%W函数的作用是对参数逐字以空格分隔并组成数组,因为使用方便,所以在Rails代码中常被使用  
  # Add additional load paths for your own custom dirs  
  # config.load\_paths += %W( #{RAILS\_ROOT}/concerns )  
#强制所有的环境使用同样的日志级别(缺省生产模式使用: info,其余的是: debug)  
  # Force all environments to use the same logger level  
  # (by default production uses :info, the others :debug)  
  # config.log\_level = :debug  
#验证cookie中session数据正确性的密匙  
  # Your secret key for verifying cookie session data integrity.  
  # If you change this key, all old sessions will become invalid!  
  # Make sure the secret is at least 30 characters and all random,  
  # no regular words or you'll be exposed to dictionary attacks.  
  config.action\_controller.session = {  
    :session\_key =&gt; '\_altered\_beast\_session',  
    :secret      =&gt; 'f471415647be47dc513d5e345ca4e582a8f99f388e0ccd46a2cdac51e2cd27c8e8b4d7dbba379cf661d4857afaf6b1867489bbc5e16b5fb14d2c3e53df64c272'  
  }  
#使用数据库存储session，替代默认的客户端cookie存储  
  # Use the database for sessions instead of the cookie-based default,  
  # which shouldn't be used to store highly confidential information  
  # (create the session table with 'rake db:sessions:create')  
  # config.action\_controller.session\_store = :active\_record\_store  
   #当创建测试数据库时,使用SQL代替Active Record,如果你的schema不能由Schema dumper完全备份时,这么做是必要的#例如,当你受限或拥有数据库特定列类型时  
  # Use SQL instead of Active Record's schema dumper when creating the test database.  
  # This is necessary if your schema can't be completely dumped by the schema dumper,  
  # like if you have constraints or database-specific column types  
  # config.active\_record.schema\_format = :sql  
#激活需要一直运行的监听器  
  # Activate observers that should always be running  
  # config.active\_record.observers = :cacher, :garbage\_collector  
 #使Active Record使用基于UTC对时区,而不是平地时间  
  # Make Active Record use UTC-base instead of local time  
  config.active\_record.default\_timezone = :utc  
#指定rails框架使用的默认I18N语言，  
  # The internationalization framework can be changed  
  # to have another default locale (standard is :en) or more load paths.  
  # All files from config/locales/\*.rb,yml are added automatically.  
  # config.i18n.load\_path &lt;&lt; Dir\[File.join(RAILS\_ROOT, 'my', 'locales', '\*.{rb,yml}')]  
  # config.i18n.default\_locale = :en

end