2

I am a front end developer and new to Ruby on Rails so please ignore my lack of naming conventions etc.

The site I am working on uses RequireJS and I am implementing Recurly payment form, I can get everything working except the signature.

The problem is that the signature I generated by the server and therefore it needs to pass though Ruby to get the code to build the transaction form, but because the application.js file is not 'erb' then it can't find the value, here is the JS:

Recurly.config({
  subdomain: "ie-webinar"
 , currency: "USD"
 , country: "US"
});

Recurly.buildTransactionForm({
    target: '#recurly-transaction'
  , successURL: '/thank_you.html'
  , signature:'<%= @signature %>'
});

Here is the Ruby code inside config > initialiser > recurly.rb

signature = Recurly.js.sign(
  :transaction => { :amount_in_cents => 19_99, :currency => 'USD' }
)

How can I solve this issue? I have experimented with creating a .js.erb file inside the view but I am unsure how to call it or get it to appear in the website header?

Dan Mitchell
  • 844
  • 2
  • 15
  • 34
  • '<%= atsignature %>' remove the single quotes, single quotes might not allow the ruby code get evaluated so use this instead: <%= atsignature %> Note replace at with the @ sign – bjhaid Mar 11 '13 at 14:03

1 Answers1

1

app/views/partials/_recurly_signature.html.erb

<%= javascript_tag do %> 
   Recurly.signature = "<%=j RECURLY_SIGNATURE %>";
<% end %>

config/initializer/recurly.rb

RECURLY_SIGNATURE = Recurly.js.sign(
  :transaction => { :amount_in_cents => 19_99, :currency => 'USD' }
)

app/views/layouts/application.html.erb

<head>
  = render :partial => "partials/recurly_signature"
</head>
gregates
  • 6,607
  • 1
  • 31
  • 31
  • Hi, thanks for the response, this method works however i get an unexpected token error in the console because of the dot between Recurly.Signature, when I remove the dot the error goes away however the script then fails. Any ideas? – Dan Mitchell Mar 11 '13 at 15:02
  • I wanted to say that probably caused by including the partial before the .js file that defines Recurly, but I then I would expect that to be a ReferenceError rather than an unexpected token. You can always just define whatever arbitrary variable you want in the partial (e.g., `foo = "<%=j RECURLY_SIGNATURE %>";` and then assign this to Recurly.signature within `Recurly.buildTransactionForm({ signature: foo });` – gregates Mar 11 '13 at 15:49
  • Great! This work fine but the issue I have is that the application.js file loads before the jquery and all other scripts including the recurly.js file so It keeps throwing an undefined error. the project is using requirejs and the asset pipeline (?). very frustrating! – Dan Mitchell Mar 11 '13 at 16:26