RailsApps / rails-stripe-membership-saas

An example Rails 4.2 app with Stripe and the Payola gem for a membership or subscription site.
http://railsapps.github.io/rails-stripe-membership-saas
1.14k stars 232 forks source link

Admin can't delete users #110

Closed laurenierullo closed 9 years ago

laurenierullo commented 9 years ago

As an admin I click 'delete user' and I get the green confirmation msg:

"User deleted."

However, the user remains there. What can I do to fix this? Why is it happening?

Austio commented 9 years ago

Could you post your destroy method here?

laurenierullo commented 9 years ago

In users_controller.rb it's:

def destroy authorize! :destroy, @user, :message => 'Not authorized as an administrator.' user = User.find(params[:id]) unless user == current_user user.destroy redirect_to users_path, :notice => "User deleted." else redirect_to users_path, :notice => "Can't delete yourself." end end

tmock12 commented 9 years ago

change user.destroy to user.destroy!

If the user can't be deleted it will raise an ActiveRecord::RecordNotDestroyed and give you a hint at why.

laurenierullo commented 9 years ago

I just checked heroku logs and it says: Stripe Error: No such customer: cus_4sXwe7fEQCawBH; a similar object exists in test mode, but a live mode key was used to make this request.

I didn't realize that users created in test mode could still be listed, but technically not exist once I switched to live. I'm able to delete users that signed up once I switched to live, but none of my testers can access their accounts. Is there a way I can delete them from the command line so they can create accounts again? Or even better, migrate them to become live users?

DanielKehoe commented 9 years ago

Check the Heroku docs on how to reset the database.

laurenierullo commented 9 years ago

I just did: heroku pg:reset DATABASE (this deleted everything!) heroku run rake db:migrate heroku run rake db:seed

Now I'm the only user. That was not my intention but I'm really new at this and am learning as I go.

Thank you so much for creating this starter app! I originally found it because I was having trouble connecting stripe with devise. :)

DanielKehoe commented 9 years ago

You're welcome. The rails-stripe-membership-saas app is fairly complex so you're doing very well. If you can, you may want to read my book Learn Ruby on Rails or look at the RailsApps tutorials for some of the simpler applications, just to get a little more comfortable.

laurenierullo commented 9 years ago

I will! Thank you! One last thing, I just created another test account, upgraded, and then deleted the account. The upgrade went through in stripe but not the deletion. This would mean a user would still get charged after supposedly deleting their account. Is there a fix for this?

FYI The app I created is www.fitnova.com :)

DanielKehoe commented 9 years ago

Take a look at the User model. A cancel_subscription method is called before the user is destroyed. Not sure why that didn't happen for you.

laurenierullo commented 9 years ago

In user.rb it says: before_destroy :cancel_subscription .... def cancel_subscription unless customer_id.nil? customer = Stripe::Customer.retrieve(customer_id) unless customer.nil? or customer.respond_to?('deleted') subscription = customer.subscriptions.data[0] if subscription.status == 'active' customer.cancel_subscription end end end

In edit.html.erb:

Cancel my account

Perminently <%= link_to "Cancel my account", registration_path(resource_name), :data => { :confirm => "Are you sure?" }, :method => :delete, :class => 'btn btn-mini' %>

DanielKehoe commented 9 years ago

Try debugging it. Put a log message in the cancel_subscription method.

DanielKehoe commented 9 years ago

See the "Troubleshoot" chapter. Or (for free): http://guides.rubyonrails.org/debugging_rails_applications.html#the-logger

laurenierullo commented 9 years ago

Just signed up. Thanks for your help.

DanielKehoe commented 9 years ago

There is a new version of this application for Rails 4.2 using the Payola gem.