30

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.

3 Answers 3

52

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.

41

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 %>
1
  • 2
    if not could be changed to unless
    – Besi
    Commented Aug 1, 2013 at 8:16
1

If you want to display something in production, but not on a certain page(s), you can do something like this:

<% if !current_page?(controller: 'home', action: 'dashboard') %>
  <% if Rails.env.production? %>
    <!-- contant go here -->
  <% end %>
<% end %>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.