我目前在Rails项目中使用Devise进行用户注册/身份验证.当用户想要取消他们的帐户时,用户对象被销毁,这使我的应用程序处于不期望的状态.
实现"软删除"的最简单方法是什么,即仅删除个人数据并将用户标记为已删除?我仍然希望保留所有记录关联.
我假设我必须首先为用户引入一个新的"已删除"列.但是后来我在用户的个人资料视图中遇到了这个默认代码:
<p>Unhappy? <%= link_to "Cancel my account", registration_path(resource_name), :confirm => "Are you sure?", :method => :delete %>.</p>
Run Code Online (Sandbox Code Playgroud)
我在哪里可以找到这种:delete方法?我该如何覆盖默认的Devise方法?
我目前在Rails项目中使用Devise进行用户注册/身份验证.当用户想要取消其帐户时,将以如下方式软删除用户对象.
我的这种方式有很小的不同.用户模型具有属性'deleted_flag'.并且,soft_delete方法执行"update_attribtue(:deleted_flag,true)"
但是,我必须实施sign_in行动.在我的implmenetation是以下.
class SessionsController < Devise::SessionsController
def create
resource = warden.authenticate!(:scope => resource_name, :recall => "#{controller_path}#new")
if resource.deleted_flag
p "deleted account : " + resource.deleted_flag.to_s
sign_out(resource)
render :controller => :users, :action => :index
else
if is_navigational_format?
if resource.sign_in_count == 1
set_flash_message(:notice, :signed_in_first_time)
else
set_flash_message(:notice, :signed_in)
end
end
sign_in(resource_name, resource)
respond_with resource, :location => redirect_location(resource_name, resource)
end
end
end
Run Code Online (Sandbox Code Playgroud)
我认为这段代码有点奇怪.
如果删除的用户试图进入,系统会允许记录并立即注销.并且,系统无法显示flash [:alert]消息...
我想知道两点.
我有一个控制器过滤器,如果他们的帐户过期,应该注销用户,但我无法找到一个简单的方法来做到这一点.
我试过了:
if user_signed_in? && current_user.status == 'expired'
redirect_to destroy_user_session_path
end
Run Code Online (Sandbox Code Playgroud)
但上述方法不起作用,因为Devise想要在注销路径上执行DELETE操作,因此您不能只重定向到它.