简体   繁体   中英

Payola issue: undefined method `amount' for nil:NilClass

I'm just learning how to use Payola to create Subscriptions for my RoR 5.1.5 test app. I'm following along with the instructions on the wiki.

I've set up the sample form taken from the example on the Wiki and dropped it right into app/views/subscriptions/new.html.erb. When I enter the credit card, email, expiration date and click Submit, I get this Rails error: undefined method `amount' for nil:NilClass

I've created the SubscriptionPlan in the rails console and confirmed it exists. I've confirmed that the new Plan shows up in my Stripe dashboard after creating it in the console.

I'm sure I've overlooked something, and hoping someone has experienced this same issue and can point me in the right direction. It feels like I am not specifying the plan anywhere. I'm not sure how I should do that.

Thanks for your help.

Code is here. 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

In your create action, you mention the SubscriptionPlan 's id like so

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

From your form in the view, I cannot see that you will submit a param called :plan_id . Perhaps post your params from the log file, to make sure the plan's id is there. Otherwise you have to tweak the form to include the plan's id. The plan will be present in the form as @plan , so you could include a hidden field:

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

Then the plan should be found. Your NoMethodError for nil with the method amount points to the problem, that the plan that the amount method is sent to, is nil . (At least that's my guess)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM