Ruby on rails开发从头来(windows)(三十二)- Rails的配置文件2011-12-03 博客园 CureRails的运行时配置是由config目录下的文件来控制的。1.运行时环境(Runtime environment)当开发者编写代码时,需求是多中多样的。在开发中你可能会需要很多的登录,加载变化过的代码。在测试时,你需要各个系统之间是隔离开的。在发布之前,你可能需要进行性能优化,并且时用户远离bug。为了支持这一切,Rials有运行时配置的概念。每一个环境都拥有自己的一组配置,可以在不同的环境中运行同一个应用程序。切换运行环境可以使我们不用修改代码就可以从开发切换到测试再切换到发布。怎样指定运行环境呢?这取决于你怎样运行你的程序,如果你使用了script/server,并且使用了-e参数:Depot > ruby script/server –e development |test| production如果你使用apche或者lighttpd,就要设置RAILS_ENV的环境变量,我们在后面再介绍。如果你有特殊的需求,你可以创建自己的环境(environment),你需要向数据库配置中添加自己的配置节,并且在config/environment文件夹中添加一个配置文件。2.配置数据库链接文件config/database.yml被用来配置数据库链接,你会发现它包括三个配置节,每个配置节都以环境名字开始,后面紧跟一个冒号。下面的行必须缩进,内容时key和对应的值,两者之间用冒号隔开。最少的情况下,每个配置节必须指定一个数据库适配器(database adapter)和使用的数据库,诸如Mysql,Postgres等等。数据库适配器有自己的特定的配置,完整的列表我们将在后面列出来。下面时我们目前depot程序的database.yml文件:
development: adapter: mysql database: depot_development username: password: host: localhost# Warning: The database defined as "test" will be erased and# re-generated from your development database when you run "rake".# Do not set this db to the same as development or production.test: adapter: mysql database: depot_test username: password: host: localhostproduction: adapter: mysql database: depot_production username: root password: prod host: wibble