0

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?

Aaron Dufall
  • 1,177
  • 10
  • 34

1 Answers1

1

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
niels
  • 1,719
  • 10
  • 20
  • Thanks, I ended up using: current_user.profile.update_attribute(:deleted, true) super will this achieve the same thing? – Aaron Dufall Jun 18 '12 at 12:42
  • Just to be sure, what is the Devise model you use for authentication? Is it User, or Profile? In my example I have used the `current_user` method, but this should be `current_#{devise_resource}`, with devise_resource `user` or `profile`. Do be carefull with passing it to `super`, this triggers the default action after having done your custom work and that would normally be really deleting the resource. – niels Jun 20 '12 at 09:36