# How to detect RAILS\_ENV in pure js file?

**URL:** <https://www.ruby-forum.com/t/how-to-detect-rails-env-in-pure-js-file/109081>\
**Category:** Rails\
**Created:** [July 11, 2007, 12:01pm UTC](https://www.ruby-forum.com/t/how-to-detect-rails-env-in-pure-js-file/109081 "2007-07-11T12:01:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rainchen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ecd19e/32.png) [@rainchen](https://www.ruby-forum.com/u/rainchen)\
**Post date:** [July 11, 2007, 12:01pm UTC](https://www.ruby-forum.com/t/how-to-detect-rails-env-in-pure-js-file/109081/1 "2007-07-11T12:01:25Z")

</div>

I need to check Rails running environment type in a js file such as  
application.js.  
I can check that like " if RAILS\_ENV == ‘development’ …" in  
a .rb/.rhtml/.rjs,but I need to do the detection in a pure javascript  
file.

I try these first (append to application.js):  
function appEnv(env)  
{  
var current\_env = ‘’;  
var scripts = $$(‘script[src=“applications.js”]’);  
if(scripts.first())  
{

```
current_env = 'production';

```

}  
else  
{  
current\_env = ‘development’;  
}  
return (env ? current\_env == env.toLowerCase() : current\_env );  
}

then I use it as:  
if(appEnv(‘development’))  
{  
// do sth  
}

or

if(appEnv() == ‘production’ )  
{  
// do sth  
}

but when I change the server running under production type.I found the  
scripts’ src still output with a time stamp string like :

not my expect as:

because I have many layout files, that would be a hell if have to  
change each one .

I have an idea is to cover the “javascript\_path” helper, to add some  
attribute make it return as:

Then I could check that tag with js.  
But I don’t how to .

Any suggestion?

-RainChen

---

<div class="post-metadata">

**Author:** ![rainchen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ecd19e/32.png) [@rainchen](https://www.ruby-forum.com/u/rainchen)\
**Post date:** [July 11, 2007, 12:39pm UTC](https://www.ruby-forum.com/t/how-to-detect-rails-env-in-pure-js-file/109081/2 "2007-07-11T12:39:01Z")

</div>

On Wed, 2007-07-11 at 03:00 -0700, RainChen wrote:

> var scripts = $$(‘script[src=“applications.js”]’);  
> }  
> {  
> script\>  
> javascript" env=“development”\>
> 
> Then I could check that tag with js.  
> But I don’t how to .
> 
> Any suggestion?

You can do javascript\_include\_tag(‘application’, :class =\> RAILS\_ENV)

I’m pretty sure “env” is not a valid attribute for script tags, but that  
only matters if you’re a purist.

–  
Tore D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![rainchen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ecd19e/32.png) [@rainchen](https://www.ruby-forum.com/u/rainchen)\
**Post date:** [July 11, 2007, 10:14pm UTC](https://www.ruby-forum.com/t/how-to-detect-rails-env-in-pure-js-file/109081/3 "2007-07-11T22:14:28Z")

</div>

I dont what to modify each layout file.Is there a better way?
