Rails 4设计重新激活"软删除"账户

时间:2016-01-24 21:21:22

标签: ruby-on-rails ruby ruby-on-rails-4 devise

遵循此tutorial on how to do a soft-delete,这有效。但是,如果用户决定要返回并重新激活帐户,那么最好的方法是什么?或者这也有维基​​?我无法找到它。

1 个答案:

答案 0 :(得分:2)

查看您发布的教程链接,他们使用时间(deleted_at)来确定是否删除了用户(软删除)。

如果登录凭据正确,您现在可以使用单独的路线和方法重新激活用户我称之为 reactivate_user

# app/models/user.rb  

  # instead of deleting, indicate the user requested a delete & timestamp it  
  def soft_delete  
    update_attribute(:deleted_at, Time.current)  
  end  

  def reactivate_user  
    update_attribute(:deleted_at, nil)  
  end 
  # ensure user account is active  
  def active_for_authentication?  
    super && !deleted_at  
  end  

  # provide a custom message for a deleted account   
  def inactive_message   
    !deleted_at ? super : :deleted_account  
  end