Rails Stripe:如何取消订阅?

时间:2015-06-20 20:49:44

标签: ruby-on-rails ruby devise

我正在使用设计和设计/编辑我已经放了一个"取消我的订阅"按钮,但我不是100%如何让它工作。

如何允许用户取消订阅条带?这是我得到的错误,任何帮助表示赞赏

enter image description here

edit.html.erb

<%= button_to "Cancel my Subscription", canceled_path, :data => { :confirm => "Are you sure?" }, :method => :delete, class: "btn btn-default btn-xs" %>

迁移到用户模型

class AddExtraDetailsToUser < ActiveRecord::Migration
  def change
      add_column :users, :subscribed, :boolean, :default => false
      add_column :users, :stripeid, :string
  end
end

的routes.rb

Rails.application.routes.draw do

    resources :subscribe

    get '/cancel_plan' => 'subscribes#cancel_plan'

    devise_for :users do
        resources :posts 
        resources :products
    end

    get 'users/:id' => 'users#show', as: :user

end

SubscribesController.rb

class SubscribesController < ApplicationController
    before_action :authenticate_user!


    def new
    end

    def update
        token = params[:stripeToken]
        customer = Stripe::Customer.create(
            :card => token,
            :plan => 2,
            :email => current_user.email               
        )

        current_user.subscribed = true
        current_user.stripeid = customer.id
        current_user.save

        redirect_to user_path, :notice => "Your subscription was setup!"
    end

def cancel_plan
    @user = current_user
    if @user.cancel_user_plan(params[:customer_id])
      @user.update_attributes(customer_id: nil, plan_id: 1)
      flash[:notice] = "Canceled subscription."
      redirect_to root_path
    else
      flash[:error] = "There was an error canceling your subscription. Please notify us."
      redirect_to edit_user_registration_path
    end
  end

end

1 个答案:

答案 0 :(得分:2)

当您的路线为:method => :delete定义get时,您的按钮会显示/cancel_plan。改变

get '/cancel_plan' => 'subscribes#cancel_plan'

delete '/cancel_plan' => 'subscribes#cancel_plan'