Ruby on rails開發從頭來(windows)(三十二)- Rails的設定檔

來源:互聯網
上載者:User

Rails的運行時配置是由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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.