[英]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.