Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails - best way to display code only in production?

I have a few pieces of code that I would like to display only in production, for instance, the showing of disqus comments. What is the best way to go about doing that? Currently I have:

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

But I am not sure if that's the best method, or is that it? Seems pretty verbose and I would need this in a few different places in the application.

like image 993
jyoseph Avatar asked Jan 08 '11 06:01

jyoseph


2 Answers

The effective check is

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

There is no need to put it as a constant in your environment.rb or an initializer. Just keep your code simple and use Rails.env.production? in your main code base I say.

like image 186
Aditya Sanghi Avatar answered Sep 22 '22 00:09

Aditya Sanghi


I'd suggest writing a helper method in your application_helper.rb file:

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

Then, in your view it gets really simple:

<%= render_disqus %> 
like image 27
Jacob Relkin Avatar answered Sep 22 '22 00:09

Jacob Relkin