[英]Destroy user session without Devise Rails 5
I will try to do a simple user authorization without Devise. 我将尝试在没有Devise的情况下进行简单的用户授权。 My session controller has new, create and destroy actions:
我的会话控制器具有新的,创建和销毁的动作:
def new
@user = User.new
end
def create
@user = User.find_by(name: user_params[:name])
@user ||= User.create(user_params)
if @user.valid? && @user.authenticate(user_params[:password])
session[:user_id] = @user.id
redirect_to tasks_path
end
end
def destroy
reset_session
end
But it doesn't work. 但这是行不通的。 How can I destroy a user session?
如何销毁用户会话?
Something like this should do it. 这样的事情应该做。 It really depends on what kind of helpers you have set up.
这实际上取决于您设置了什么样的助手。 You'll need access to the session variable you created and also the user id, so
session.delete(@user.id)
might end up being what you need. 您将需要访问您创建的会话变量以及用户ID,因此
session.delete(@user.id)
可能最终就是您所需要的。 Depends on how this is being sent over to the method and what data you have access to (is there a before action or something providing the @user variable or do you need to find it with something like @user = User.find(:params)
where params is the user id. 取决于将其发送到方法的方式以及您有权访问哪些数据(是否存在操作之前或提供@user变量的东西,还是需要使用类似
@user = User.find(:params)
东西来查找它)? @user = User.find(:params)
,其中params是用户ID。
def destroy
session.delete(:user_id)
@user = nil
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.