Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 476
rornoob Avatar asked Dec 30 '09 23:12

rornoob


People also ask

What are .ERB files?

Script written in ERB, a templating language for Ruby; may include any type of plain text or source code, but also includes Ruby ERB code that generates additional text into the resulting file when run with the ERB template engine. ERB is often used for templating Web files such as . RB, .

How do ERB files work?

An ERB object works by building a chunk of Ruby code that will output the completed template when run. If safe_level is set to a non-nil value, ERB code will be run in a separate thread with $SAFE set to the provided level. eoutvar can be used to set the name of the variable ERB will build up its output in.

What is an ERB template?

An ERB template looks like a plain-text document interspersed with tags containing Ruby code. When evaluated, this tagged code can modify text in the template. Puppet passes data to templates via special objects and variables, which you can use in the tagged Ruby code to control the templates' output.


1 Answers

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
like image 198
klochner Avatar answered Nov 02 '22 14:11

klochner