3

Is there anyway I can get an environment variable (like an API key) to appear inside JS/Coffee using Rails 3.1?

1
  • The given answer is good, but you can also set a global JS variable from your view like (HAML): :javascript \n window.API_KEY = '#{ENV['API_KEY']}'; Commented May 26, 2017 at 3:11

1 Answer 1

9

You can rename your file.js to file.js.erb and use ruby to access ENV:

<% do_something_with ENV['key'] %>
some_js_code
Sign up to request clarification or add additional context in comments.

1 Comment

Should work if properly named. See guides.rubyonrails.org/… section 2.2.3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.