2011-01-08 36 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?在我的主要代碼庫中,我說。