Permalink
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
20 lines (19 sloc) 750 Bytes
layout title jumbo
post
Pass Rails ENV to Front End
ENV['BASE_URL']

Sometimes you need ENV values from rails in your front end. Maybe API_BASE_URL or a MAPBOX_TOKEN. Here is a simple approach, make an env.js.erb file in assets and set it up as follows.

var ENV = {
  API_BASE_URL: '<%= ENV['API_BASE_URL'] %>',
  BASE_URL: '<%= ENV['BASE_URL'] %>',
  MAPBOX_TOKEN: '<%= ENV['MAPBOX_TOKEN'] %>',
}

Because JS and Ruby are so similar you can access the values anywhere from the front end the same way you do in Ruby.

var token = ENV['MAPBOX_TOKEN'];

Note that you need to be careful what values you make available to your front end and make sure you only provide values that can be publicized.