Payola问题:未定义的方法`amount'为nil:NilClass

时间:2018-03-20 16:15:53

标签: ruby-on-rails payola

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

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

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

我确信我已经忽视了某些事情,并希望有人遇到同样的问题,并能指出我正确的方向。感觉就像我没有在任何地方指定计划。我不知道该怎么做。

感谢您的帮助。

代码就在这里。 应用程序/模型/ 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

1 个答案:

答案 0 :(得分: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 nil的{​​{1}}指向问题,amount方法发送到的planamount。 (至少那是我的猜测)