【问题标题】:Payola issue: undefined method `amount' for nil:NilClassPayola 问题:nil:NilClass 的未定义方法“金额”
【发布时间】:2018-03-24 04:58:45
【问题描述】:

我正在学习如何使用 Payola 为我的 RoR 5.1.5 测试应用创建订阅。我按照 wiki 上的说明进行操作。

我已经设置了从 Wiki 上的示例中获取的示例表单,并将其直接放入 app/views/subscriptions/new.html.erb。当我输入信用卡、电子邮件、到期日期并单击提交时,我收到此 Rails 错误:undefined method `amount' for nil:NilClass

我已经在 rails 控制台中创建了 SubscriptionPlan 并确认它存在。 我已确认在控制台中创建新计划后,它会显示在我的 Stripe 仪表板中。

我确定我忽略了一些东西,希望有人遇到过同样的问题并能指出我正确的方向。感觉就像我没有在任何地方指定计划。我不知道该怎么做。

感谢您的帮助。

代码在这里。 app/models/subscription_plan.rb

class SubscriptionPlan < ActiveRecord::Base
  include Payola::Plan
end

/app/controllers/subscriptions_controller.rb

class SubscriptionsController < ApplicationController
  # bring in the `render_payola_status` helper.
  include Payola::StatusBehavior

  def new
    @plan = SubscriptionPlan.first
  end

  def create
    # do any required setup here, including finding or creating the owner object
    owner = current_user # this is just an example for Devise

    # set your plan in the params hash
    params[:plan] = SubscriptionPlan.find_by(id: params[:plan_id])

    # call Payola::CreateSubscription
    subscription = Payola::CreateSubscription.call(params, owner)

    # Render the status json that Payola's javascript expects
    render_payola_status(subscription)
  end
end

/app/views/subscriptions/new.html.erb

 <!-- this header can go in <head> or at the bottom of <body> -->
<%= render 'payola/transactions/stripe_header' %>

<%= form_tag('/subscriptions',
      class: 'payola-onestep-subscription-form',
      'data-payola-base-path' => '/payola',
      'data-payola-plan-type' => @plan.plan_class,
      'data-payola-plan-id' => @plan.id
  ) do |f| %>
  <span class="payola-payment-error"></span>
  Email:<br>
  <input type="email" name="stripeEmail" data-payola="email"></input><br>
  Card Number<br>
  <input type="text" data-stripe="number"></input><br>
  Exp Month<br>
  <input type="text" data-stripe="exp_month"></input><br>
  Exp Year<br>
  <input type="text" data-stripe="exp_year"></input><br>
  CVC<br>
  <input type="text" data-stripe="cvc"></input><br>
  <input type="submit"></input>
<% end %>

/app/config/routes.rb

Rails.application.routes.draw do
  devise_for :users
  root to: "pages#index"
  resources :subscriptions
  get 'pages/index'

  get 'pages/donate'

  mount Payola::Engine => '/payola', as: :payola
  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end

【问题讨论】:

  • 堆栈跟踪/调用者堆栈会很有帮助。在不知道错误发生在哪里的情况下,我们无能为力。

标签: ruby-on-rails payola


【解决方案1】:

在您的 create 操作中,您提到了 SubscriptionPlan 的 ID,如下所示

# set your plan in the params hash
params[:plan] = SubscriptionPlan.find_by(id: params[:plan_id])

从视图中的表单中,我看不到您将提交一个名为:plan_id 的参数。也许从日志文件中发布您的参数,以确保计划的 id 在那里。否则,您必须调整表单以包含计划的 ID。该计划将以@plan 的形式显示,因此您可以包含一个隐藏字段:

<%= hidden_field_tag(:plan_id, @plan.id) %>

然后应该找到计划。您的NoMethodError for nilamount 方法指出了问题所在,amount 方法发送到的plannil。 (至少这是我的猜测)

【讨论】:

  • 谢谢。你为我指明了正确的方向。如上所示,我必须添加隐藏字段标记,但我手动添加了我之前创建的计划的 plan.id 的整数。我没有一个完整的站点,其中包含可用于填充 @plan.id 变量的变量的产品。感谢您的指导。
  • 酷!总是乐于提供帮助。
猜你喜欢
  • 2021-07-22
  • 2018-10-15
  • 2011-07-27
  • 2013-06-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多