Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I could advise overriding <code>destroy</code> method on your User model to simply do <code>update_attribute(:deleted_at, Time.current)</code> (instead of actually destroying), but this deviation from standard API could become burdensome in the future, so here's how to modify the controller.</p> <p>Devise has a bunch of default controllers out of the box. The best way to customize them is to create your own controller inheriting the corresponding devise controller. In this case we are talking about <code>Devise::RegistrationsController</code> — which is easily recognized by looking at source. So create a new controller.</p> <pre><code>class RegistrationsController &lt; Devise::RegistrationsController end </code></pre> <p>Now we have our own controller fully inheriting all the devise-provided logic. Next step is to tell devise to use it instead of the default one. In your routes you have <code>devise_for</code> line. It should be changed to include registrations controller.</p> <pre><code>devise_for :users, :controllers =&gt; { :registrations =&gt; 'registrations' } </code></pre> <p>This seems strange, but it makes sense because by default it's 'devise/registrations', not simply 'registrations'.</p> <p>Next step is to override the <code>destroy</code> action in registrations controller. When you use <code>registration_path(:user), :method =&gt; :delete</code> — that's where it links. To <code>destroy</code> action of registrations controller.</p> <p>Currently devise does the following.</p> <pre><code>def destroy resource.destroy set_flash_message :notice, :destroyed sign_out_and_redirect(self.resource) end </code></pre> <p>We can instead use this code. First let's add new method to <code>User</code> model.</p> <pre><code>class User &lt; ActiveRecord::Base def soft_delete # assuming you have deleted_at column added already update_attribute(:deleted_at, Time.current) end end # Use this for Devise 2.1.0 and newer versions class RegistrationsController &lt; Devise::RegistrationsController def destroy resource.soft_delete 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 end # Use this for older Devise versions class RegistrationsController &lt; Devise::RegistrationsController def destroy resource.soft_delete set_flash_message :notice, :destroyed sign_out_and_redirect(resource) end end </code></pre> <p>Now you should be all set. Use scopes to filter out deleted users.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload