【问题标题】:How do I access a variable created in CoffeeScript in my rails model? [duplicate]如何在我的 rails 模型中访问在 CoffeeScript 中创建的变量? [复制]
【发布时间】:2015-04-14 21:22:43
【问题描述】:

我正在使用条带在 rails 4 应用程序上创建客户帐户。

基本上,我知道用于捕获 cc 信息(如下)的咖啡脚本代码正在与 Stripe 成功通信,因为正在为卡片创建令牌(创建后我可以将其打印到屏幕上)。

同样,我的模型中有一个方法可以在 Stripe 中创建客户对象。它连接并以条带显示,当我尝试对信用卡信息进行硬编码(即传递带有预设参数的块)时,卡信息以条带显示。

但是,我不知道如何将卡令牌从我的咖啡脚本代码传递到我的模型(或控制器,或者我真正可以真正使用它的任何地方)。如果有人可以帮助我,或者向我指出一些关于它的文档,那就太棒了!

这是我的咖啡脚本代码,其中令牌保存为“#subscription_stripe_card_token”:

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

这是我的模型,我尝试(不成功)访问它(在 save_with_payment 方法中,调用 Stripe::Customer.create,是我尝试传递卡信息的地方):

    class Subscription
  include Mongoid::Document
  belongs_to :user
  field :plan, type: String
  field :stripe_customer_token, type: String

  attr_accessor :stripe_card_token

  def save_with_payment(plan, email, name)
    if valid?
        customer = Stripe::Customer.create(description: name, email: email, plan: plan, card: stripe_card_token)
        write_attribute(:stripe_customer_token, customer.id)
        save!
    end
    rescue Stripe::InvalidRequestError => e
    logger.error "Stripe error while creating customer: #{e.message}"
    errors.add :base, "There was a problem with your credit card."
    false
  end

同样,其他一切正常。目前我假设它传递了一个“nil”值,我不知道如何访问它。

【问题讨论】:

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


    【解决方案1】:

    你不能。 CoffeeScript(编译为 JavaScript)在用户的浏览器中运行。 Rails 在您的服务器上运行。他们无法访问彼此的状态。

    它们之间唯一可以进行的通信形式是客户端发起的 HTTP 请求。

    您需要通过 AJAX 将您的条带令牌发送到您的服务器。

    【讨论】:

    • 我该怎么做?
    猜你喜欢
    • 1970-01-01
    • 2011-05-06
    • 1970-01-01
    • 2011-12-27
    • 1970-01-01
    • 1970-01-01
    • 2014-11-14
    • 1970-01-01
    • 2016-10-29
    相关资源
    最近更新 更多