How to include a value from .env file in Rails into javascript tag using Slim Framework

44 Views Asked by At

Consider we have an ENV defined in the Rails .env file and we want to use that in the .html.slim file under the Javascript section

.env file

NUMBER_TO_MULTIPLY: 2

filename.html.slim

javascript:
  $(document).ready(function() {
    var count = selectedRows.length; // Gives me value as 3
    var totalCount = count * "#{ENV['NUMBER_TO_MULTIPLY']}" // Should give me value as 3 * 2 = 6
    alert(totalCount) // Produces me 0 as the result 
  }

I tried Rails.env but it give the value as "development" apart from that I am not able to access any value from the ENV. Any help would be greatly appreciated.

1

There are 1 best solutions below

3
Arctodus On

.env files arent supported by Rails out of the box.

For development / test the dotenv gem is an alternative.

For production it depends on the hosting environment.