【问题标题】:Getting Coffeescript to Find the Right Div // Stripe Error让 Coffeescript 找到正确的 div // 条纹错误
【发布时间】:2012-09-27 12:15:39
【问题描述】:

在 Ryan Bates 的教程的帮助下,我能够设置 Stripe。现在,我试图让用户更新他们的信用卡信息。

现在我很想在单击操作按钮时调用条带。造成这种情况的可能原因是,我通过基本上复制 Ryan 的代码来制作新信用卡,将咖啡脚本破解了。

这是我用来输入新信用卡信息的表格

<%= form_tag("/users/update_card", :method => "put", :class => "edit_user", :id => "change_card" ) do %>
  <%= hidden_field_tag :stripe_card_token %>

  <div id="stripe_error" class="alert">   
      <noscript>JavaScript is not enabled and is required for this form. First enable it in your web browser settings.</noscript>
  </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 on Card (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>


  <%= submit_tag("Update My Credit Card", :class => "button") %>

这是我正在使用的 Coffeescript。前半部分包括新用户的注册(有效),后半部分从 changecard.setupForm() 开始,没有

# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/

jQuery ->
  Stripe.setPublishableKey($('meta[name="stripe-key"]').attr('content'))
  subscription.setupForm()

subscription =
  setupForm: ->
    $('#new_user').submit ->
      $('input[type=submit]').attr('disabled', true)
      if $('#card_number').length
        subscription.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, subscription.handleStripeResponse)

  handleStripeResponse: (status, response) ->
    if status == 200
      $('#user_stripe_card_token').val(response.id)
      $('#new_user')[0].submit()
    else
      $('#stripe_error').text(response.error.message)
      $('input[type=submit]').attr('disabled', false)

changecard.setupForm()

changecard =
  setupForm: ->
    $('#change_card').submit ->
      $('input[type=submit]').attr('disabled', true)
      if $('#card_number').length
        subscription.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, subscription.handleStripeResponse)

  handleStripeResponse: (status, response) ->
    if status == 200
      $('#user_stripe_card_token').val(response.id)
      $('#change_card')[0].submit()
    else
      $('#stripe_error').text(response.error.message)
      $('input[type=submit]').attr('disabled', false)

这是我的 update_card 操作

  def update_card
  @user = current_user
  cu = Stripe::Customer.retrieve(@user.stripe_customer_token)
  cu.card = params[:stripe_customer_token] # obtained with Stripe.js
  cu.save
  redirect_to edit_user_path(@user)
  end

感谢您坚持到这里!非常感谢您的想法。

编辑

单步执行javascript后,它暂停了

changecard.setupForm()  

这就是我从 :P 开始的那一行。错误是

uncaught TypeError: Cannot call method 'setupForm' of undefined

似乎没有使用我上面放置的咖啡脚本正确定义 changecard。我不确定如何更改它,非常感谢任何帮助!

编辑 2

通过javascript调试,这是最终代码,似乎传递了stripe_card_token

**# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/
jQuery ->
  Stripe.setPublishableKey($('meta[name="stripe-key"]').attr('content'))
  subscription.setupForm()
  changecard.setupForm()
subscription =
  setupForm: ->
    $('#new_user').submit ->
      $('input[type=submit]').attr('disabled', true)
      if $('#card_number').length
        subscription.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, subscription.handleStripeResponse)

  handleStripeResponse: (status, response) ->
    if status == 200
      $('#user_stripe_card_token').val(response.id)
      $('#new_user')[0].submit()
    else
      $('#stripe_error').text(response.error.message)
      $('input[type=submit]').attr('disabled', false)
changecard =
  setupForm: ->
    $('#change_card').submit ->
      $('input[type=submit]').attr('disabled', true)
      if $('#card_number').length
        changecard.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, changecard.handleStripeResponse)

  handleStripeResponse: (status, response) ->
    if status == 200
      $('#stripe_card_token').val(response.id)
      $('#change_card')[0].submit()
    else
      $('#stripe_error').text(response.error.message)
      $('input[type=submit]').attr('disabled', false)**

【问题讨论】:

  • 您遇到了什么错误?
  • 嗨 Beerlington,stripe_card_token 在提交时似乎没有被填写。就好像从 changecard 开始的咖啡脚本被忽略了——即按钮没有禁用,如果我什么都不输入,则不会出现错误。我可以提供一些信息来帮助更好地揭示错误吗?
  • 您是否遇到 JS 或 Ruby 错误?
  • 您是否尝试过使用 firebug 或等效的 JS 调试工具单步执行 Javascript?
  • 您在控制台中看到任何 JS 错误吗?

标签: javascript ruby-on-rails coffeescript stripe-payments


【解决方案1】:

编辑 2

通过 javascript 调试,这是最终代码,似乎传递了 stripe_card_token

**# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/
jQuery ->
  Stripe.setPublishableKey($('meta[name="stripe-key"]').attr('content'))
  subscription.setupForm()
  changecard.setupForm()
subscription =
  setupForm: ->
    $('#new_user').submit ->
      $('input[type=submit]').attr('disabled', true)
      if $('#card_number').length
        subscription.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, subscription.handleStripeResponse)

  handleStripeResponse: (status, response) ->
    if status == 200
      $('#user_stripe_card_token').val(response.id)
      $('#new_user')[0].submit()
    else
      $('#stripe_error').text(response.error.message)
      $('input[type=submit]').attr('disabled', false)
changecard =
  setupForm: ->
    $('#change_card').submit ->
      $('input[type=submit]').attr('disabled', true)
      if $('#card_number').length
        changecard.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, changecard.handleStripeResponse)

  handleStripeResponse: (status, response) ->
    if status == 200
      $('#stripe_card_token').val(response.id)
      $('#change_card')[0].submit()
    else
      $('#stripe_error').text(response.error.message)
      $('input[type=submit]').attr('disabled', false)**

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-22
    • 2023-03-04
    • 2019-01-27
    • 2011-05-24
    • 1970-01-01
    • 2013-01-17
    • 1970-01-01
    相关资源
    最近更新 更多