Joshua Garza
Joshua Garza

Reputation: 1

Redirect to a view after deleting account in Rails/Devise

I'm new to Rails & I have a page where the user can cancel their account using button_to. However, how can I redirect the user to another page? For example, I would want to redirect to a post-cancellation page saying "goodbye" upon deletion of their account.

I tried looking to see if there was an after_user_deletion devise method to override but there is not.

Here is what I have in the edit view under registrations from Devise.

    <div id="modal">
        <p>Are you sure you want to cancel?</p>
        <%= button_to "Yes", registration_path(resource_name), method: :delete%>
        <button>No</button>
    </div>

Im not sure if it's necessary to make another controller but I made a cancellation controller.

class CancellationController < ApplicationController
    before_action :authenticate_user!

    def cancel
    
    end

end

Here is what I have in routes.rb

Rails.application.routes.draw do
    devise_for :users
    root to: 'dashboard#index'
    devise_scope :user do
         get 'cancel' => 'cancellation#cancel'

    end

end

Here is the contents of rake routes:

RakeRoutes

Upvotes: 0

Views: 415

Answers (1)

Guilherme Pereira
Guilherme Pereira

Reputation: 366

You need to override the redirect on the devise registration controller:

 # config/routes.rb

 devise_for :users, controllers: { registrations: 'registrations' }

This will point to your controllers folder now! So you need to create your own devise controller:

# app/controllers/registrations_controller.rb

class RegistrationsController < Devise::RegistrationsController
  # DELETE /resource
  def destroy
    # your own logic here, so you can redirect it after
  end

  def cancel
    expire_data_after_sign_in!
    redirect_to here_your_url
  end
end

Note: You are using the DELETE on your button, that triggers the destroy not the cancel, you can check the devise controller here.

Upvotes: 1

Related Questions