How to access Rails configuration info in Backbone View?

103 views Asked by At

I'm trying to implement Stripe into a Rails app with a Backbone front end.

In a normal rails view, I can do this:

<%= form_tag charges_path do %>
  <article>
    <label class="amount">
      <span>Amount: $5.00</span>
    </label>
  </article>

  <script src="https://checkout.stripe.com/checkout.js" class="stripe-button"
          data-key="<%= Rails.configuration.stripe[:publishable_key] %>"
          data-description="Launch Survey"
          data-amount="<%= @amount %>"></script>
<% end %>

And on clicking the button it will trigger the Stripe form to enter in the credit card information.

I want to transition this to a Backbone view, specifically in a jst.eco template. I'm wondering what the best way is to get the Rails.configuration.stripe[:publishable_key] from my server to my view?

Usually for things like this I'd just create a cookie, but that doesn't seem safe with this type of payment related information.

Thanks!

1

There are 1 answers

1
Manikandan On BEST ANSWER

We can store this config values in Javascript through application.html.erb Include this script in applcation.html.erb

<script type="text/javascript">
    App.Util.key = "<%= Rails.configuration.stripe[:publishable_key] %>";
</script>

Now you can use this key in JS.