未定义的方法`金额'为零:与Payola的NilClass

时间:2017-09-17 03:23:15

标签: ruby-on-rails stripe-payments payola

我一直在关注Payola的本教程。

我已经测试过添加自己的计划,它们会出现在我的信息中心和控制台上。但是,当我去创建订阅时,我收到以下错误:

undefined method `amount' for nil:NilClass

在以下一行:

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

以下是我的参考文件:

订阅/ new.html.erb

<h1>New Subscription</h1>
<em><%= @plan.name %><em/><br><br>

<!-- 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>
  <input name="stripeEmail" data-payola="email" type="hidden" value="<%= current_user.email %>"/>
  Card Number<br>
  <input type="text" data-stripe="number"></input><br><br>
  Exp Month<br>
  <input type="text" data-stripe="exp_month"></input><br><br>
  Exp Year<br>
  <input type="text" data-stripe="exp_year"></input><br><br>
  CVC<br>
  <input type="text" data-stripe="cvc"></input><br><br>
  <input type="submit"></input>
<% end %>

subscriptions_controller.erb

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

  before_action :load_plans

  def index
  end 

  def new
    @plan = EmojiPlan.find(params[:plan_id])
  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] = EmojiPlan.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

  protected

  def load_plans
    @plans = EmojiPlan.order('amount')
  end
end

的routes.rb

Rails.application.routes.draw do
resources :emoji_plans
resources :subscriptions
devise_for :admins
devise_for :users
mount Payola::Engine => '/payola', as: :payola
# For details on the DSL available within this file, see 
http://guides.rubyonrails.org/routing.html
end

emoji_plan.rb

class EmojiPlan < ApplicationRecord
include Payola::Plan

def redirect_path(subscription)
    # you can return any path here, possibly referencing the given subscription
    '/'
end
end

create_emoji_plans.rb

class CreateEmojiPlans < ActiveRecord::Migration[5.1]
  def change
    create_table :emoji_plans do |t|
      t.integer :amount
      t.string :interval
      t.string :stripe_id
      t.string :name

      t.timestamps
    end
  end
end

不要取笑我的计划名称 - 我正在制作一个有趣的&#39;项目。 ;)

0 个答案:

没有答案