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
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.
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.
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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With