Skip to content Skip to sidebar Skip to footer

Using Dotenv In Production, Or Passing Env Variables To Js/js.erb? And Attempt With Credentials

I am attempting to pass my ENV variable of my stripe publishable key to the stripe.js file for Connect. Dotenv gem: gem 'dotenv-rails', groups: [:development, :test, :production]

Solution 1:

If you have remembered to precompile your assets (which I often forget when dealing with heroku), you will need to add the js.erb file to the precompile list explicitly (SO post, more detailed blog post)

In this case though, you actually do not need to wrap this up in a variable anyway. The publishable key can be (should be) included to identify your Stripe account and the interpolation is going to send it in plain text anyway (what you normally want to avoid by using ENV variables) so you can skip the interp step entirely.

Edit from comments: You need to verify that the env key is actually set on Heroku. There are two ways to check:

  • login to the CLI and check
  • check also through the settings in the dashboard (this has examples on how to do both)

If you're getting an empty string in the compiled JS then that would at least indicate that the ERB is getting processed so the error is likely that there is nothing to interpolate when it is.

Just to reiterate there's no reason to hide a the publishable key (that's why it's called that and why the docs pass it in the clear). Furthermore putting it in an ENV that gets interpolated into text isn't hiding anything anyway.


Solution 2:

I think you have forgotten to set RAILS_MASTER_KEY environment variable in your Heroku app while using credentials approach. From your codebase assign content of master.key to it.


Post a Comment for "Using Dotenv In Production, Or Passing Env Variables To Js/js.erb? And Attempt With Credentials"