Ruby on rails “如何修复此错误”;未定义的方法`编码';对于零:NilClassa“;取消订阅计划是否有效?

Ruby on rails “如何修复此错误”;未定义的方法`编码';对于零:NilClassa“;取消订阅计划是否有效?,ruby-on-rails,stripe-payments,Ruby On Rails,Stripe Payments,这是我第一次使用Stripe和Rails,现在我尝试允许高级用户取消订阅 我可以用我的代码将用户从标准级别升级到高级级别,但是当我尝试将高级用户降级到标准级别时,我遇到了问题 我遵循了“取消订阅”的Stripe Ruby API引用,但当我单击“取消订阅”按钮时出现了此错误: NoMethodError-nil:NilClass的未定义方法encoding' /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/Ruby/2

这是我第一次使用Stripe和Rails,现在我尝试允许高级用户取消订阅

我可以用我的代码将用户从标准级别升级到高级级别,但是当我尝试将高级用户降级到标准级别时,我遇到了问题

我遵循了“取消订阅”的Stripe Ruby API引用,但当我单击“取消订阅”按钮时出现了此错误:

NoMethodError-nil:NilClass的未定义方法
encoding'
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/Ruby/2.0.0/cgi/util.rb:7:in
escape' stripe(1.21.0)lib/stripe/list_object.rb:19:in
retrieve'
app/controllers/subscriptions\u controller.rb:55:in
grade'

我的rails版本是4.2.1

我的代码:

class SubscriptionsController < ApplicationController

def create
    subscription = Subscription.new
      stripe_sub = nil
    if current_user.stripe_customer_id.blank?
      # Creates a Stripe Customer object, for associating with the charge
      customer = Stripe::Customer.create(
        email: current_user.email,
        card: params[:stripeToken],
        plan: 'premium_plan'
        )
      current_user.stripe_customer_id = customer.id
      current_user.save!
      stripe_sub = customer.subscriptions.first
    else
      customer = Stripe::Customer.retrieve(current_user.stripe_customer_id)
      stripe_sub = customer.subscriptions.create(
        plan: 'premium_plan'
        )
    end

    current_user.subid = stripe_sub.id

    current_user.subscription.save!

    update_user_to_premium
    flash[:success] = "Thank you for your subscription!"

    redirect_to root_path 

    # Handle exceptions
    rescue Stripe::CardError => e
     flash[:error] = e.message
     redirect_to new_subscriptions_path
  end


  def downgrade

    customer = Stripe::Customer.retrieve(current_user.stripe_customer_id)
    customer.subscriptions.retrieve(current_user.subid).delete

    downgrade_user_to_standard
    flash[:success] = "Sorry to see you go."
    redirect_to user_path(current_user)

  end
end
任何帮助都将不胜感激

更新:
感谢stacksonstacks的帮助,我所需要的只是在“current_user.subid=stripe_sub.id”下断言“subscription.user=current_user.subscription”,然后在降级方法中使用“subscription=current_user.subscription”调用subscription id。现在取消订阅工作

似乎
当前用户。subid
在此行返回
nil

customer.subscriptions.retrieve(current_user.subid).delete
您为
当前用户分配
subid
,但从未保存更改。 您只保存新创建的
订阅

current_user.subid = stripe_sub.id
current_user.subscription.save!
如果添加
current\u user.save我认为这将解决问题


希望这有帮助

谢谢你的回答!我添加了“当前用户。保存!”在我的创建方法中的“current_user.stripe_customer_id=customer.id”下,但我仍然得到了相同的错误。您需要将其移动到此行下方
current_user.subid=stripe_sub.id
,否则它将不会保存新的
subid
我还尝试将该行移动到“current_user.subid=stripe_sub.id”下,但同样的问题仍然存在。我的想法是,也许我应该在降级方法中的“customer.subscriptions.retrieve(current_user.subid).delete”下添加“subscription.delete(或subscription.save!)”?
customer.subscriptions.retrieve(current_user.subid).delete
current_user.subid = stripe_sub.id
current_user.subscription.save!