【发布时间】:2012-03-09 19:35:10
【问题描述】:
如标题所示,我已确保所有需要的 JS 文件都包含在元标记中,包括 jquery-rails/coffee-rails gem,并链接到外部文件以防万一。
这是来自 Ryan Bates 的 Stripe 教程修改后的脚本,据我了解,它应该是通过检测表单中的提交操作来启动的。
由于我对 JS 或 CoffeeScript 不太熟悉,因此我无法准确指出这里的问题所在。
如果我能得到任何帮助,我将不胜感激。
donations.js 文件:
jQuery ->
Stripe.setPublishableKey($('meta[name="stripe-key"]').attr('content'))
donation.setupForm()
donation =
setupForm: ->
$('#new_donation').submit ->
$('input[type=submit]').attr('disabled', true)
if $('#card_number').length
donation.processCard()
false
else
true
processCard: ->
card =
number: $('#card_number').val()
cvc: $('#card_code').val()
expMonth: $('#card_month').val()
expYear: $('#card_year').val()
Stripe.createToken(card, donation.handleStripeResponse)
handleStripeResponse: (status, response) ->
if status == 200
alert(response.id)
else
alert(response.error.message)
捐赠/new.html.erb 表格
<%= form_for(@donation) do |f| %>
<div class="field">
<%= f.label :from %>
<%= f.text_field :from %>
</div>
<div class="field">
<%= label_tag :card_number, "Credit Card Number" %>
<%= text_field_tag :card_number, nil, :name => nil %>
</div>
<div class="field">
<%= label_tag :card_code, "Security Code (CVV)" %>
<%= text_field_tag :card_code, nil, :name => nil %>
</div>
<div class="field">
<%= label_tag :card_month, "Card Expiration" %>
<%= select_month nil, {:add_month_numbers => true}, {:name => nil, :id => "card_month"} %>
<%= select_year nil, {:start_year => Date.today.year, :end_year => Date.today.year+15}, {:name => nil, :id => "card_year"} %>
</div>
<div class="field">
<%= f.label :Notes %>
<%= f.text_field :note %>
</div>
<br>
<center>
<span class="BigBold">Amount: $2.50</span>
<%= f.hidden_field :amount, {:value => "2,5" } %><br>
</center>
<br>
<center>
<%= f.submit "Donate Now", :class => 'donate-button' %>
</center>
<% end %>
【问题讨论】:
标签: javascript jquery ruby-on-rails forms coffeescript