Chapter 1 :
Rails Environments and Configuration
这一章 主要讲基本的配置一些东西,我随便的做了些笔记,加上了一些补充说明。希望对
新手有帮助。或者你可以当作Tips来看:)Enjoy.
一:Enviroment Mode
Rails 有三种models:test,development,production,默认为development,你可以通过RAILS_EVN指定特定的环境
譬如
启动webrick
CODE:
ruby script/server webrick -p 80 -e production启动mongrel
CODE:
mongrel_rails start -p 80 -e production执行rake
CODE:
rake db:migrate RAILS_EVN=production二:Rails
Gem Version
/config/environment.rb
CODE:
RAILS_GEM_VERSION = '2.0.2' unless defined? RAILS_GEM_VERSION升级rails
CODE:
raile rails:update会更新config,javascript,script,如果你只想升级单个请执行
CODE:
rake rails:update:configs
rake rails:update:javascripts
rake rails:update:scripts三:Initializer
Rails 2 新增了initializers目录,在启动的时候会加载该目录的文件。这样就不需要把一大陀的东西全塞在environment里了。
四:Default Load Paths
/vendor/rails/railties/lib/initializer.rb
CODE:
def default_load_paths
paths = ["#{root_path}/test/mocks/#{environment}"]
# Add the app's controller directory
paths.concat(Dir["#{root_path}/app/controllers/"])
# Then components subdirectories.
paths.concat(Dir["#{root_path}/components/[_a-z]*"])
# Followed by the standard includes.
paths.concat %w(
app
app/models
app/controllers
app/helpers
app/services
components
config
lib
vendor
).map { |dir| "#{root_path}/#{dir}" }.select { |dir| File.directory?(dir) }
paths.concat builtin_directories
end这个是rails默认加载的path,如果你想加载自己的代码到path中,可以配置在environment.rb中
/config/environment.rb
CODE:
Rails::Initializer.run do |config|
#....
config.load_paths += %W(
vendor/BlueCloth-1.0.0/lib
vendor/RedCloth-3.0.4/lib
vendor/rubypants-0.2.0/lib
vendor/rubyzip-0.5.12/lib
vendor/uuidtools/lib
)
#.....
end这样的话,你就可以把gems直接放到vendor目录下了,不需要用户去安装了。

五:Builtin Rails Info
在本机上你可以通过http://localhost:port/rails/info/properties查看,在代码中你可以通过
CODE:
@properties = Rails::Info.properties获取。
举个例子:
简单的显示如下:
CODE:
<ul>
<%@properties.each_with_index do |p,index|%>
<li><%=p[0]%> : <%=p[1]%></li>
<%end%>
</ul>六:Configuration
上面已经提过一些,这里我们再说一些
rails 默认加载如下的framework
CODE:
def default_frameworks
[ :active_record, :action_controller, :action_view, :action_mailer, :active_resource ]
end你可以忽略你不需要加载的
CODE:
config.frameworks -= [:action_mailer ]七:Log-Level Override
production模式下默认使用info,其他使用debug,你可以修改如下配置
CODE:
config.log_level = :debug八:ActiveRecord Session Store
rails 2 默认是保存在客户端的,如果你想的话,可以采用其他的方式,譬如数据库存储
CODE:
config.action_controller.session_store = :active_record_storeNote:
如果你还没有表,请使用
CODE:
rake db:sessions:create
rake db:migrate生成表结构
九:Observers
CODE:
config.active_record.observers = :cacher, :garbage_collector在enviroment.rb中,observers 一般配置为cache 的sweeper,或者gc等
十:Automatic Class Reloading
production 默认是cache_classes的,如果你想auto reload,请修改/config/environments/production.rb
CODE:
config.cache_classes = falsedevelopment 默认为false,即当代码有变化时自动重新加载
十一:查看已加载的path
CODE:
ruby script/console
Loading development environment (Rails 2.0.2)
>> $:
#....results...........十二:Logging
rails 中默认有个全局变量RAILS_DEFAULT_LOGGER,你可以通过defined?查看是否定义
CODE:
unless defined?(RAILS_DEFAULT_LOGGER) do
#create new logger ...
end十三:Rails Log Files
清除log日志
CODE:
rake log:clearPS:同样也有tmp:clear等,用来清除tmp
十四:Log File Analysis
http://rails-analyzer.rubyforge.org/pl_analyze/+
http://nubyonrails.com/articles/ ... -for-the-rest-of-us[
本帖最后由 martin 于 2008-1-15 15:44 编辑]