Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 461
david Avatar asked May 03 '12 23:05

david


People also ask

Where is environment js in Rails?

The config/webpack/environment. js file is where the default webpack configuration is imported via @rails/webpacker . The named import environment is an abstraction around the webpack config. It provides its own API to support modification and extension.

How do I see environment variables in Rails?

Use command ENV in rails console. That will return a hash of your environmental values you can access. Alternatively, you can access your environmental variables from your apps root path using the same command and the variables will be returned formatted.

Where do I put JavaScript code in Rails?

The directory structure for JavaScript has changed to the app/javascript/packs/ folder. In that folder you will find the application. js file, which is just like the application.

Where do I put JavaScript in a rails app?

The directory structure for JavaScript has changed to the app/javascript/packs/ folder. In that folder you will find the application.js file, which is just like the application.css file. It will be imported by default in the application.html.erb file when you create your new Rails app.

How do I use ENV in a Rails application?

Use ENV ["GMAIL_USERNAME"] anywhere in a Rails application. Your application won’t know if it was loaded from the config/local_env.yml file or from the Unix shell. Occasionally you’ll want to use different account credentials or API keys for test and development environments.

Where can I use environment variables in rails?

The variable can be used anywhere in a Rails application. Ruby will replace ENV ["GMAIL_USERNAME"] with an environment variable. Let’s consider how to set local environment variables. If you’re familiar with Unix, you’ve likely had experience setting environment variables.

What is the default compiler for JavaScript in rails 6?

Prior to Rails 6, we had the asset pipeline to manage CSS and JavaScript files. But starting from Rails 6, Webpacker is the default compiler for JavaScript. CSS is still managed by the asset pipeline, but you can also use Webpack to compile CSS.


1 Answers

You dont need to pass it into your javascript file directly. You can do it in the erb view file like this for example:

<script>
  window._rails_env = "<%= Rails.env %>"
</script>

or better this:

<%= javascript_tag do %>
  window._rails_env = "<%= Rails.env %>"
<% end %>

or the best (IMHO) this:

<body data-env="<%= Rails.env %>">
  ...

and then:

var railsEnv = $('body').data('env')

Warning:

Dumping your entire Rails environment in script like this will almost certainly compromise your web app's security! As Michał Zalewski mentions in a comment below, your Rails application has sensitive information in its environment, like database credentials, API keys, application secrets for signing and encrypting cookies etc.

like image 150
Matthew Avatar answered Oct 16 '22 07:10

Matthew