我有两个迁移表:父母和老师。我使用Bcrypt进行注册。我无法弄清楚我应该用登录和sessions_controller(会话帮助器)做什么。我可以注册新用户,当用户注册时,他只能在导航栏中看到注销链接。但是,我无法注销用户,我不确定如何在会话控制器和会话助手中定义方法。也许有人可以帮我这个?我无法找到有关不同用户模型的bcrypt的信息 - 这个东西不受欢迎还是这么容易,我只是愚蠢而且不懂事情?
class SessionsController < ApplicationController
include SessionsHelper
def new
end
def create
teacher = Teacher.find_by(email: params[:session][:email])
parent = Parent.find_by(email: params[:session][:email])
if teacher && teacher.authenticate(params[:session][:password])
log_in teacher
redirect_to documents_path
flash[:notice] = "Welcome!"
elsif parent && parent.authenticate(params[:session][:password])
log_in parent
redirect_to root_path
flash[:notice] = "Welcome!"
else
flash[:alert] = "Please log in again!"
render 'new'
end
end
def destroy
if log_out parent
redirect_to root_path
elsif log_out teacher
redirect_to root_path
end
end
end
这是我的会议助手:
module SessionsHelper
# Logs in the given user.
def log_in(parent)
session[:parent_id] = parent.id
end
def log_in(teacher)
session[:teacher_id] = teacher.id
end
# Returns the current logged-in user (if any).
def current_teacher
@current_teacher ||= Teacher.find_by(id: session[:teacher_id])
end
def current_parent
@current_parent ||= Parent.find_by(id: session[:parent_id])
end
# Returns true if the user is logged in, false otherwise.
def logged_in?(teacher)
!current_teacher.nil?
end
def logged_in?(parent)
!current_parent.nil?
end
def log_out(teacher)
session.delete(:teacher_id)
@current_teacher = nil
end
def log_out(parent)
session.delete(:parent_id)
@current_parent = nil
end
end
我不知道你的申请的细节,但我可以解释一般情况。
首先,具有登录功能的控制器不必命名为sessions_controller
,名称可以。
Bcrypt基本上只是一个用于加密密码的库。主要流程是检查用户输入的密码是否有效而无需解密。如何实现控制器逻辑没有明确的答案。
显然,用户分为两种类型,教师和父母,可能每个都有不同的功能。基本上,我想将两个登录进程划分为单独的控制器或操作。因为每个登录过程都不一样。
但是,如果用户由于UI限制必须从同一页面登录,则Teacher和Parent将使用相同的URL登录。如果您处于这种情况下,在相同的控制器和操作中实施将是适当的。
毕竟,这取决于如何设计您的应用程序。所以你的代码并不总是错的。
但是,看看你的代码,教师或家长只能通过电子邮件判断,这是否是一个正确的方法是值得怀疑的。我没有看到许多网站,其中具有不同权限的用户从同一页面登录。
我认为它基本上是根据教师或家长划分登录页面。如果您划分登录页面,示例如下。
class TeachersController < ApplicationController
include SessionsHelper
def login
end
def login_action
teacher = Teacher.find_by(email: params[:teacher][:email])
if teacher && teacher.authenticate(params[:teacher][:password])
log_in teacher
flash[:notice] = 'Welcome!'
redirect_to root_path
else
flash[:notice] = 'Invalid log in information!'
redirect_to action: :login
end
end
def logout
teacher = Teacher.find(session[:teacher_id])
log_out teacher
redirect_to root_path
end
end
class ParentsController < ApplicationController
include SessionsHelper
def login
end
def login_action
parent = Parent.find_by(email: params[:parent][:email])
if parent && parent.authenticate(params[:parent][:password])
log_in parent
flash[:notice] = 'Welcome!'
redirect_to root_path
else
flash[:notice] = 'Invalid log in information!'
redirect_to action: :login
end
end
def logout
parent = Parent.find(session[:parent_id])
log_out parent
redirect_to root_path
end
end
虽然这不是主要问题,但您是否在helpers
目录中编写了sessions_helper?
通常,helper用于实现视图逻辑,因此如果要在控制器中共享方法,请在ActiveSupport::Concern
目录中使用concerns
。