2015-06-20 4 views
1

Я использую devise и в разработке/редактировании. Я положил кнопку «отменить мою подписку», но я не на 100%, как заставить ее работать.Rails Stripe: Как отменить подписку?

Как разрешить пользователям отказаться от подписки на полосу? Здесь ошибка я получил, любая помощь приветствуется

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 

Маршруты. 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 

ответ

2

Ваша кнопка имеет :method => :delete в то время как ваши маршруты определяет get для /cancel_plan. Изменение

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

к

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

Любая идея, почему я получаю 'неопределенный метод cancel_plan для # <Пользователь: 0x007fa7f6db9200>'? Что я могу сделать по этому поводу? – JamesRocky

+1

Я не уверен, где вы получили 'cancel_path' из вашей кнопки. Я думаю, что это должно быть 'cancel_plan_path'. – forthowin

+0

Извините, я обновил его после вашего ответа. Я должен изменить вопросы, чтобы сделать их точными. Хотя с этим является 'cancel_plan_path', я все еще получаю неопределенную ошибку метода – JamesRocky

Смежные вопросы