Rails之道---><The Rails Way> 摘录(1)Rails环境与配置

1.每当你启动一个进程(例如Webrick服务器)处理Rails请求的时候,第一件发生的事情就是加载config/enviroment.rb
例如publich/dispatch.rb文件的顶部
require File.dirname(_FILE_) + "/../config/environment"


2.[quote]# 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'[/quote]
如果你将RAILS_ENV设置为生产模式,HUOZHE 或者修改常量RAILS_ENV,它将使Rails程序中的一切运行于生产模式.
例如,test_helper.rb,我们可以看到它在加载环境配置之前是先把RAILS_ENV设置为测试模式的,因此它将不能正常工作.
ENV["RAILS_ENV"] = "test"
require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
require 'test_help'


3.脚本enviroment.rb的首要任务是寻找Rails框架并加载它.
# Specifies gem version of Rails to use when vendor/rails is not present
RAILS_GEM_VERSION = '2.0.2' unless defined? RAILS_GEM_VERSION

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

4.enviroment.rb之后的这一行,在加载config/boot.rb后,才真正启动了Rails
# Bootstrap the Rails environment, frameworks, and default configuration
require File.join(File.dirname(__FILE__), 'boot')

(备注:这个启动脚本是Rails应用程序生成的一部分,但不能修改.它能够协助你检查Rails的安装是否有问题)

5.在Ruby中,通常你想从不同的文件中加载代码到你的应用程序中时,你可以用包含一条require的语句实现
工作机制:
(1)如果类或模组并非嵌套定义,则在常量名之间插入一个下划线并加载这个名字对应的文件.
如:
[quote]AboutMy变为require/about_my/
YouKnow变为require/you_know/[/quote]
(2)如果类或模组是嵌套定义,那么Rails在包含的每个模组之间插入一个下划线并从对应子目录中加载相应的文件.

[quote]RpanZa::ErYaDe变为require/rpan_za/er_ya_de[/quote]

6.在开发模式下启动任何Rails应用程序并访问http://localhost:3000/rails/info/properties.你会看到一些信息

7.config/environment.rb


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.
#config.gem "authlogic"

# 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 ]
跳过你不使用的框架(仅当使用vendor/rails 时有效)

# 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 ]

# Add additional load paths for your own custom dirs
# config.load_paths += %W( #{RAILS_ROOT}/extras )
为自己的默认路径添加另外的加载路径(备注:%W函数的作用是对参数逐字以空格分隔并组成数组,因为使用方便,所以在Rails代码中常被使用.)

# Force all environments to use the same logger level
# (by default production uses :info, the others :debug)
# config.log_level = :debug
强制所有的环境使用同样的日志级别(缺省生产模式使用: info,其余的是: debug)

# 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 => '_Faq_session',
:secret => '59c4e510413d743345309bd5ccd7d5e957c3cc0e9b78bb00eb57e0797870884659aa44771f14b1c0124059093aca669d0f421cfcb85e40f34c79f0d9aea35581'
}

# config.gem "rspec", :lib => false, :version => ">= 1.2.9"
# config.gem "rspec-rails", :lib => false, :version => ">= 1.2.9"

#config.gem 'mislav-will_paginate', :lib => 'will_paginate', :source => 'http://gems.github.com'
# 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
使用数据库代替文件系统做session(使用"rake db:sessions:create"创建Session表)

# 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
当创建测试数据库时,使用SQL代替Active Record,如果你的schema不能由Schema dumper完全备份时,这么做是必要的,例如,当你受限或拥有数据库特定列类型时

# Activate observers that should always be running
# config.active_record.observers = :cacher, :garbage_collector
激活需要一直运行的监听器

# Make Active Record use UTC-base instead of local time
# config.active_record.default_timezone = :utc
使Active Record使用基于UTC对时区,而不是平地时间

#ActiveRecord::Base.record_timestamps = false
end



8.config/environments/development.rb
# Settings specified here will take precedence over those in config/environment.rb

# In the development environment your application's code is reloaded on
# every request. This slows down response time but is perfect for development
# since you don't have to restart the webserver when you make code changes.
config.cache_classes = false
在开发环境中,应用程序的代码需要在每次请求时都重新加载.这样会延长响应时间,但是如果在代码改变不需重启网络服务器时,这种配置就会对开发有利.
(备注:当config_cache_class设置为true时,Rails将使用Ruby的require语句加载类,如果此选项为false时,Rails将使用load替代.)
#想要查看项目加载路径的内容,只需打开控制台并输入$:

>>$:
=>.............


# Log error messages when you accidentally call methods on nil.
config.whiny_nils = true
当碰巧调用了nil的方法时记录错误信息

# Show full error reports and disable caching
config.action_controller.consider_all_requests_local = true
config.action_view.debug_rjs = true
config.action_controller.perform_caching = false
config.action_view.cache_template_extensions = false
显示完整的错误报告,并使缓存无效

# Don't care if the mailer can't send
config.action_mailer.raise_delivery_errors = false
如果邮件未发送,不必在意.


test.rb
# Settings specified here will take precedence over those in config/environment.rb

# The test environment is used exclusively to run your application's
# test suite. You never need to work with it otherwise. Remember that
# your test database is "scratch space" for the test suite and is wiped
# and recreated between test runs. Don't rely on the data there!
config.cache_classes = true
#(这里可以看到development里的事false,原因如下-->)
测试数据库是"scratch space",是专为测试集准备的,使其在测试运行时擦除和重建,不要依赖该库中的数据.

# Log error messages when you accidentally call methods on nil.
config.whiny_nils = true

# Show full error reports and disable caching
config.action_controller.consider_all_requests_local = true
config.action_controller.perform_caching = false

# Disable request forgery protection in test environment
config.action_controller.allow_forgery_protection = false

# Tell ActionMailer not to deliver emails to the real world.
# The :test delivery method accumulates sent emails in the
# ActionMailer::Base.deliveries array.
config.action_mailer.delivery_method = :test
让ActionMailer 不发送实际的emails,:test目录方法在ActionMailer::Base.deliveries数组中累积发送emails


production.rb
# Settings specified here will take precedence over those in config/environment.rb

# The production environment is meant for finished, "live" apps.
# Code is not reloaded between requests
config.cache_classes = true
生产环境意味着完成'live'程序,请求的间隔中代码不会被重载.

# Use a different logger for distributed setups
# config.logger = SyslogLogger.new
为分布式安装使用不同的登录器

# Full error reports are disabled and caching is turned on
config.action_controller.consider_all_requests_local = false
config.action_controller.perform_caching = true
config.action_view.cache_template_loading = true
打开缓存,同时令完整的错误报告失效.

# Enable serving of images, stylesheets, and javascripts from an asset server
# config.action_controller.asset_host = "http://assets.example.com"
使stylesheets,images和javascript文件开始服务

# Disable delivery errors, bad email addresses will be ignored
# config.action_mailer.raise_delivery_errors = false
使错误输出失效,因此错的email地址会被忽略.



9.DIY(如有必要可以通过copy config/enviroments目录中已存在的环境配置文件为Rails应用程序创建额外的环境配置.绝大多数自定义环境都是用来添加更多的产品配置,比如staging和QA部署)
在开发模式中使用普通的环境设定,但将数据库连接到生产数据库服务器.当你有需要快速诊断生产环境中的问题时,这将是一个急救的组合方式.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值