Skip to content
Advertisement

how to get environment in javascript file in rails app

hello ive been trying to get the current environment in rails but i think im doing something wrong with my javascript, but i dont seem to know what. in my application.js i have…

var rails_env = '<%= Rails.env -%>';
alert(rails_env);
alert(rails_env.value);
if(rails_env == 'development'){
    alert('inside if')
    var indexName = "idx";
}
else{
    alert('inside else')
     var indexName = "idx_production";
}

it always goes into my else statement even if i am in development mode. what am i doing wrong? thank you

how to get environment in javascript file in rails app

Advertisement

Answer

Rails’ asset pipeline will only preprocess assets that you mark for parsing. Whether those assets are going to end up CSS, JS, or whatever else, you can mark files for parsing by adjusting the file extension.

In this case, where you’re trying to output an ERB variable, you will need to change the extension of the file to application.js.erb.

There’s an extended discussion of the preprocessor here.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement