Saving a set of environment variables in rails and javascript

essentially, we want to keep the set of constants that will be used in the rails and javascript application. For instance:

{A:3 B:4 C:5} 

We try not to embed rails code in javascript, and we do not need 2 copies of constants.

Thanks!

+4
source share
3 answers

To avoid defining your constants twice, I would use Rails to create a JS that contains your constants. I understand that I want to avoid using Rails in JS, but proper code separation should cover any such problems.

First define your constants in Ruby, for example, in config/environment.rb or in a custom initializer:

 CONST_A = 3 CONST_B = 4 CONST_C = 5 

Then create a new controller with one action, the sole purpose of which is to read the Ruby values ​​and output them as JavaScript. For instance:

 class JavascriptsController < ApplicationController caches_page :constants def constants render :template => 'constants.js.erb' end end 

Note that the action is cached by the page to avoid unnecessary hits on the Rails stack.

Now create a view app/views/javascripts/constants.js.erb in which you display the Ruby values ​​as a JavaScript object:

 var constants = { a: <%= CONST_A %>, b: <%= CONST_B %>, c: <%= CONST_C %> }; 

Connect a simple route in config/routes.rb to connect to JavascriptsController actions:

 map.connect '/javascripts/:action.js', :controller => 'javascripts' 

Finally, include this JS in your layout before any other JS:

 <%= javascript_include_tag 'constants' %> 

That should do it. This line requests /javascripts/constants.js , which then defines the JS constants object. Although this JS is generated using Rails, it remains separate from any other JS files in the application and avoids duplicate definitions of your constants.

As mentioned earlier, you must cache the JavascriptsController#constants action. Also consider minimizing constants.js (i.e., removing unnecessary characters) and combining it with other JavaScript files to reduce HTTP requests. I use the AssetHat gem (disclaimer: I wrote it), which has successfully accelerated CSS and JS on some high-profile sites.

+2
source

put it in a JSON file and parse it in a ruby ​​file and in a javascript file.

+3
source

I did something similar.

 class Constants DUCK = "Quack" CAT = "Meow" PROGRAMMER = "Tap,tap,tap." def self.make_javascript output = "" self.constants.each do |c| output += "var #{c} = \"#{self.send(c)}\";\n" end return output end end 

Then in your layout file, somewhere in your head write:

 <script type='text/javascript'> <%= Constants.make_javascript %> </script> 

This makes it so that adding a constant to the class automatically adds it to javascript.

As written, my code only deals with string constants. You can enter a constant type check and adjust the output accordingly.

+1
source

Source: https://habr.com/ru/post/1305048/


All Articles