`
xxj
  • 浏览: 420741 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

The Rails Way 第一章笔记

阅读更多
Chapter 1 : Rails Environments and Configuration

这一章 主要讲基本的配置一些东西,我随便的做了些笔记,加上了一些补充说明。希望对新手有帮助。或者你可以当作Tips来看:)Enjoy.

一:Enviroment Mode
Rails 有三种models:test,development,production,默认为development,你可以通过RAILS_EVN指定特定的环境
譬如
启动webrick
ruby script/server webrick -p 80 -e production

启动mongrel
mongrel_rails start -p 80 -e production 

执行rake
rake db:migrate RAILS_EVN=production


二:Rails Gem Version
/config/environment.rb
RAILS_GEM_VERSION = '2.0.2' unless defined? RAILS_GEM_VERSION


升级rails
raile rails:update

会更新config,javascript,script,如果你只想升级单个请执行
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
      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

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查看,在代码中你可以通过
@properties = Rails::Info.properties

获取。

举个例子:
简单的显示如下:
<ul>
<%@properties.each_with_index do |p,index|%>
  <li><%=p[0]%> : <%=p[1]%></li>
<%end%>
</ul>


六:Configuration
上面已经提过一些,这里我们再说一些
rails 默认加载如下的framework
      def default_frameworks
        [ :active_record, :action_controller, :action_view, :action_mailer, :active_resource ]
      end


你可以忽略你不需要加载的
config.frameworks -= [:action_mailer ]



七:Log-Level Override
production模式下默认使用info,其他使用debug,你可以修改如下配置
config.log_level = :debug


八:ActiveRecord Session Store
rails 2 默认是保存在客户端的,如果你想的话,可以采用其他的方式,譬如数据库存储
config.action_controller.session_store = :active_record_store


Note:
如果你还没有表,请使用
rake db:sessions:create
rake db:migrate

生成表结构


九:Observers
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
config.cache_classes = false


development 默认为false,即当代码有变化时自动重新加载

十一:查看已加载的path
ruby script/console
Loading development environment (Rails 2.0.2)
>> $:
#....results...........


十二:Logging
rails 中默认有个全局变量RAILS_DEFAULT_LOGGER,你可以通过defined?查看是否定义
unless defined?(RAILS_DEFAULT_LOGGER) do
   #create new logger ...
end


十三:Rails Log Files
清除log日志
rake log:clear


PS:同样也有tmp:clear等,用来清除tmp


十四:Log File Analysis
http://rails-analyzer.rubyforge.org/pl_analyze/
+
http://nubyonrails.com/articles/a-hodel-3000-compliant-logger-for-the-rest-of-us
分享到:
评论
1 楼 blackanger 2008-10-24  
很可惜没有看到你第二章的笔记

相关推荐

Global site tag (gtag.js) - Google Analytics