【问题标题】:How can I get the destroy action in my user controller to work properly?如何让我的用户控制器中的销毁操作正常工作?
【发布时间】:2012-07-16 04:03:14
【问题描述】:

我正在使用 Stripe,我想确保当用户在我的网站上删除他或她的帐户时,该用户的信息和订阅会自动从 Stripe 中删除。现在,在我的用户控制器中,我有以下销毁操作的代码:

 def destroy
    User.find(params[:id]).destroy
    flash[:success] = "Your account has been deleted."
    redirect_to root_path
  end

在 Stripe 的网站上,他们有以下代码用于删除订阅了订阅的客户:

cu = Stripe::Customer.retrieve("cus_IzJx2FQ7r13IOk")
cu.delete

我想做的是将该代码放在我的 rails 应用程序的销毁操作中。我不确定如何从 Stripe 中正确检索与即将删除的客户 ID 相关的客户 ID。我客户的 Stripe id 设置为 stripe_customer_token,并且 stripe_customer_token 的值存储在我的数据库中。您可以在设置此功能方面给我的任何帮助都很棒!

【问题讨论】:

    标签: ruby-on-rails


    【解决方案1】:

    您可以先删除 Stripe 关联,而不是直接销毁用户实例,如下所示。

    def destroy
      @user = User.find(params[:id])
      cu = Stripe::Customer.retrieve(@user.stripe_customer_token)
      cu.delete
      @user.destroy
      flash[:success] = "Your account has been deleted."
      redirect_to root_path
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-07-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多