rornoob
rornoob

Reputation: 53

modify erb file on deployment

I have a fragment of JavaScript that I want to add to a page, but only in the production environment. Does rails have a way to insert or conditionally include on deployment. I know I could do "if Rails.env.production?" But I'd rather not do this condition check every time the page is loaded.

Upvotes: 5

Views: 461

Answers (2)

Ben
Ben

Reputation: 6965

What I do in this situation is create a constant in each environment's config file:

#config/environments/development.rb
SNIPPET = ""

#config/environments/production.rb
SNIPPET = "<script src='whatever.js'></script>"

#app/views/file.html.erb
<%= SNIPPET %>

Upvotes: 1

klochner
klochner

Reputation: 8125

I wouldn't be worried about the overhead of one if statement.

Why not use a custom helper method:


def snippet
  if RAILS_ENV == "production"
   javascript_tag "whatever"
  elsif . . .
end
then you can use the same syntax:

<%= snippet %>

and you get a couple benefits:

  • access to other rails helpers
  • your config file won't be littered with raw html

Upvotes: 5

Related Questions