0

So I am trying to get my code to match Stripe's js example:

var stripe = Stripe('pk_test_REST_OF_MY_KEY');

Here are the lines from my .js.erb file that call the right key from my secrets.yml file. When this renders I get the following error in the browser console Uncaught ReferenceError: pk_test_REST_OF_MY_KEY is not defined

  var stripe = Stripe(
    <% if Rails.env == 'production' %>
      <%= Rails.application.secrets.stripe(['publishable_key']).second[1].to_s %>
    <% else %>
      <%= Rails.application.secrets.stripe(['publishable_key']).first[1].to_s %>
    <% end %>
  );

I've tried

... Stripe(` 
  RUBY LINES BETWEEN BACKTICKS 
`);

... Stripe(' + 
  RUBY LINES BETWEEN PLUSES 
+ ');

So it has to be some finicky js syntax with the (' '); not accepting the ruby value as a string, right? We know the ruby is running because the console error is printing the right value.

Also, the ruby is correct because it produces Rails.application.secrets.stripe(['publishable_key']).first[1].to_s => "pk_test_REST_OF_MY_KEY" in the console

4
  • What's the actual JS that gets generated as a result? Commented Apr 4, 2017 at 15:44
  • You know @SergioTulentsev I'm new to working with js/front end. Would I look under Inspect > Elements or Page Source for that? Commented Apr 4, 2017 at 15:55
  • Yes, page source, for example. Commented Apr 4, 2017 at 15:59
  • 1
    You'll want to use View Source or inspect the server response in your dev tools' Network pane. The parsed DOM in the Elements pane will not reflect the actual source returned by the server. Commented Apr 4, 2017 at 16:04

2 Answers 2

1

First of all, it will be helpful to clean up your code a bit to see what's happening where. Move the logic up to the top of the file:

<%
  config = Rails.application.secrets.stripe(['publishable_key'])
  stripe_key = Rails.env.production? ? config.first[1] : config.second[1]
%>

...or, better, yet, a helper:

def stripe_key
  config = Rails.application.secrets.stripe(['publishable_key'])
  Rails.env.production? ? config.first[1] : config.second[1]
end

Then, in your JavaScript:

var stripe = Stripe('<%= j(stripe_key) %>');

// ...or...

var stripe = Stripe(<%= stripe_key.to_json %>);

Be careful to note the presence or absence of single-quotes in both cases. The j helper will escape special characters (including quotes and newlines) inside the string, but the returned string won't be wrapped in quotation marks, whereas to_json will return a string already wrapped with double-quotes.

P.S. When you say this:

Rails.application.secrets.stripe(['publishable_key'])

...are you sure you don't mean:

Rails.application.secrets.stripe['publishable_key']

# ...or...

Rails.application.secrets.stripe.publishable_key
Sign up to request clarification or add additional context in comments.

Comments

1

I think i should just be:

var stripe = Stripe(
  <% if Rails.env == 'production' %>
    '<%= Rails.application.secrets.stripe(['publishable_key']).second[1].to_s %>'
  <% else %>
    '<%= Rails.application.secrets.stripe(['publishable_key']).first[1].to_s %>'
  <% end %>
);

3 Comments

Thanks for helping out. That produces this error only generation of JSON objects or arrays allowed from the view, which prevents the view from loading. It also results in some crazy syntax highlighting: imgur.com/xyGUzxs
Hmm now it looks like it is trying to run the right line, but '<%= j(Rails.application.secrets.stripe(['publishable_key']).first[1].to_s) %>' but still outputting this error only generation of JSON objects or arrays allowed
Alright, if I drop the j() and keep the ticks on each <%= %> line it seems to work!

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.