[英]Configuring variables depending on current Rails.env
配置不同變量的最簡單方法是什么(例如,我在標簽link_to中有變量,根據當前的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"%>
我想將這個if-elsif語句移動到其他地方,或者甚至將它刪除為某種“配置文件”(如果有的話)
這有一個很棒的寶石,名為rails_config
: github repo
但是,如果您只想配置此變量並且只需要配置此變量,則可以在rails已具有的環境配置文件中創建常量。
就像是:
在config / environments / production.rb中
# production.rb
Rails.configuration.my_awesome_changing_domain = "somedomain.me"
在config / environments / development.rb中
# development.rb
Rails.configuration.my_awesome_changing_domain = "stackoverflow.com"
在config / environments / staging.rb中
# staging.rb
Rails.configuration.my_awesome_changing_domain = "news.ycombinator.com"
其他方法在另一個答案中討論了這個問題: https : //stackoverflow.com/a/5053882
最好的祝福
這是一篇關於在Rails上設置變量的好文章: http : //railsapps.github.com/rails-environment-variables.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.