`
fireflyman
  • 浏览: 118324 次
  • 性别: Icon_minigender_1
  • 来自: 火星
社区版块
存档分类
最新评论

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

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


2.
引用
# 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'

如果你将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)如果类或模组并非嵌套定义,则在常量名之间插入一个下划线并加载这个名字对应的文件.
如:
引用
AboutMy变为require/about_my/
YouKnow变为require/you_know/

(2)如果类或模组是嵌套定义,那么Rails在包含的每个模组之间插入一个下划线并从对应子目录中加载相应的文件.

引用
RpanZa::ErYaDe变为require/rpan_za/er_ya_de


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部署)
在开发模式中使用普通的环境设定,但将数据库连接到生产数据库服务器.当你有需要快速诊断生产环境中的问题时,这将是一个急救的组合方式.
2
1
分享到:
评论

相关推荐

    rails-bootstrap-modals:这个应用程序展示了如何在 rails 4 中使用模态

    &lt;div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true"&gt; &lt;div class="modal-dialog" role="document"&gt; &lt;div class="modal-content"&gt; &lt;div ...

    The Rails 5 Way-Leanpub(2017).pdf

    Since the API documentation is liberally licensed (just like the rest of Rails), there are some sections of the book that draw from the API documentation. But in practically all of those cases, the ...

    The Rails 4 Way

    ### 关于《The Rails 4 Way》的知识点总结 #### 标题:The Rails 4 Way 这本书主要讲述了Ruby on Rails 4版本的核心特性和最佳实践。Ruby on Rails(简称Rails)是一个用Ruby语言编写的开源全栈Web应用框架。本书...

    Obie Fernandez, Kevin Faustino, Vitaly Kushner - The Rails 4 Way - 2014

    ##### 第1章:Rails环境与配置 - **Bundler**: 解释了如何使用Bundler管理项目的依赖关系,确保应用能在不同环境中一致运行。 - **启动与应用设置**: 深入探讨Rails应用程序启动过程中的关键设置项,如`config/...

    rails-dev-box, 面向 Ruby on Rails 核心开发的虚拟机.zip

    rails-dev-box, 面向 Ruby on Rails 核心开发的虚拟机 用于 Ruby on Rails 核心开发的虚拟机简介注意:这个虚拟机不是为 Rails 应用程序开发而设计的,只是为。 这个项目自动设置开发环境,以便在 Ruby on Rails ...

    ProCamera2D

    - Camera Window &gt;&gt;DEMO - Cinematics &gt;&gt;DEMO - Content Fitter &gt;&gt;DEMO - Forward Focus &gt;&gt;DEMO - Geometry Boundaries - Limit Distance &gt;&gt;DEMO - Limit Speed &gt;&gt;DEMO - Numeric Boundaries &gt;&gt;DEMO - Pan ...

    rails-documentation-1-2-1.zip

    标题 "rails-documentation-1-2-1.zip" 暗示这是一份关于 Ruby on Rails 框架的文档,版本为 1.2.1。Ruby 是一种面向对象的编程语言,而 Rails 是一个基于 Ruby 的开源 Web 应用程序框架,遵循 Model-View-...

    Ruby on Rails Guides v2 - Ruby on Rails 4.2.5

    ### Ruby on Rails Guides v2 - Ruby on Rails 4.2.5 #### 一、重要概念及基础假设 - **重要概念**:本指南旨在帮助读者深入理解Ruby on Rails(以下简称Rails)4.2.5版本的核心功能与最佳实践。 - **基础假设**:...

    关于rails 3.1 cucumber-rails 1.2.0

    Cucumber-Rails集成了Cucumber与Rails,使得开发者能够在Rails环境中方便地使用Cucumber进行功能测试。 在 Rails 应用中使用 Cucumber-Rails,开发者可以创建一个名为`features`的目录,里面包含这些Gherkin特性...

    the rails way

    《The Rails Way》一书由Obie Fernandez撰写,属于Addison-Wesley Professional Ruby系列,该系列致力于为读者提供实用、面向人且深入的信息,帮助他们利用Ruby平台创建动态技术解决方案。这一系列书籍的创立基于一...

    Web开发敏捷之道--应用Rails进行敏捷Web开发 之 Depot代码。

    标题中的“Web开发敏捷之道--应用Rails进行敏捷Web开发 之 Depot代码”表明这是一个关于使用Ruby on Rails框架进行敏捷Web开发的示例项目,名为Depot。Ruby on Rails(简称Rails)是一个开源的Web应用程序框架,它...

    rails-react-components-源码.rar

    通过深入研究"rails-react-components-源码",开发者不仅可以掌握Rails与React的整合技巧,还能了解到现代Web开发的前沿实践,为自己的技术栈添加宝贵的实战经验。在实际项目中,这些知识将帮助我们构建出更高效、更...

    rails应用--导航栏实例工程

    在本项目"rails应用--导航栏实例工程"中,我们将探讨如何在Ruby on Rails框架下构建一个实用的导航栏。Rails是一个流行的开源Web应用程序框架,它遵循MVC(模型-视图-控制器)架构模式,使得开发过程更加高效且结构...

    rails-4.2.0-gems

    总的来说,Rails 4.2.0和Ruby 4.2.0的组合为开发者提供了强大而稳定的开发平台,而`rails_setup`文件则是一个宝贵的资源,帮助开发者快速搭建和配置环境。通过学习和理解这些知识点,开发者可以更高效地利用Rails...

    rails-2.1.0-gem

    "rails-2.1.0-gem"是Rails框架的一个特定版本,即2.1.0的gem包,用于在Ruby环境中安装和管理Rails框架。 Rails的核心理念是“约定优于配置”(Convention over Configuration),这意味着开发者可以遵循一套预设的...

    rails-documentation-1-2-0-rc1.chm

    rails-documentation-1-2-0-rc1.chm

Global site tag (gtag.js) - Google Analytics