2011-01-08 69 views
26

我有几段代码,我只想在生产中显示,例如,显示disqus注释。什么是最好的方式去做呢?目前我有:Rails - 仅在生产中显示代码的最佳方式?

<% if RAILS_ENV.eql?('production') %> 
    disqus code here 
<% end %> 

但我不知道这是最好的方法,还是它呢?看起来相当冗长,我需要在应用程序的几个不同的地方。

回答

39

我建议你application_helper.rb文件写入一个辅助方法:

def render_disqus 
    return '' unless Rails.env.production? 
    #render disqus stuff here... 
end 

然后,在你看来它变得非常简单:

<%= render_disqus %> 
+0

剽窃者! :) 但是不要紧! – 2011-01-08 08:18:42

45

有效检查

<% if Rails.env.production? %> 
    disqus code here 
<% end %> 

有没有必要把它作为一个常数在你的environment.rb或初始化。只需保持简单的代码并使用Rails.env.production?在我的主要代码库中,我说。