2012-12-19 28 views
3

什麼是最簡單的方法來配置不同的變量(例如我有變量在標籤link_to,哪些變化,取決於當前的Rails.env)?根據當前配置變量Rails.env

<% 
    if Rails.env == 'development' 
     domain = 'somedomain.me' 
    elsif Rails.env == 'staging' 
     domain = 'example.com' 
    end 
    %> 
    <%= link_to 'Back', root_url(:subdomain => 'www', :domain => domain), :class=>"btn btn-primary"%> 

我想這一點,如果移動 - ELSIF語句到別的地方,甚至放棄它的某種「配置文件」,如果存在的話

+0

你能和更新你的問題你想要完成什麼具體的例子? –

+0

更新了代碼示例 –

回答

3

有隻是這一個真棒寶石,叫rails_configgithub repo

但是,如果你只是想配置這個,只有這個變量,你可以在rails已經有的環境配置文件中創建一個常量。

喜歡的東西:

在你的config /環境/ production.rb

# production.rb 
Rails.configuration.my_awesome_changing_domain = "somedomain.me" 

在你的config /環境/ development.rb

# development.rb 
Rails.configuration.my_awesome_changing_domain = "stackoverflow.com" 

在你的config /環境/分期.rb

# staging.rb 
Rails.configuration.my_awesome_changing_domain = "news.ycombinator.com" 

其他的方法來做到這一點在其他的答案中討論,在此線程: https://stackoverflow.com/a/5053882

問候

+0

謝謝,這將幫助我 –