Aaron Dufall
Aaron Dufall

Reputation: 1177

Devise: How to customize the registrations controller destroy method

I have my user and profiles in separate models. When a user is deleted the linked profile remains, which is the desired result. what I would like to do is flag the profile record as deleted.

I have added a deleted column(boolean) to my profile table, but can't figure out how I can add the set to true set to the devise destroy method?

app\controller\registrations_controller.rb

class RegistrationsController < Devise::RegistrationsController
   def destroy
     delete_profile(params) 
   end


   private

   def delete_profile(params)
     profile = Profile.find(params[:id])
     profile.deleted = true
   end  
end

but I can figure out how to get around this error

Couldn't find Profile without an ID

how can I pass in the correct params from the delete user in my views?

Upvotes: 0

Views: 1910

Answers (1)

niels
niels

Reputation: 1729

Devise doesn't use a params[:id] to destroy the current user (so it isn't provided through the route), but instead uses the current_user.

Here are the relevant parts of the controller:

class Devise::RegistrationsController < DeviseController
  prepend_before_filter :authenticate_scope!, :only => [:edit, :update, :destroy]

  def destroy
    resource.destroy
    Devise.sign_out_all_scopes ? sign_out : sign_out(resource_name)
    set_flash_message :notice, :destroyed if is_navigational_format?
    respond_with_navigational(resource){ redirect_to after_sign_out_path_for(resource_name)       }
  end

  protected 

  def authenticate_scope!
    send(:"authenticate_#{resource_name}!", :force => true)
    self.resource = send(:"current_#{resource_name}")
  end
end

So your alternative would be to do something like

class RegistrationsController < Devise::RegistrationsController
  def destroy
    current_user.deleted = true
    current_user.save
    #some more stuff
  end
end

Upvotes: 1

Related Questions