查看具有类似错误的线程,其中没有一个为我提供了解决方案。在过滤到我的user controller
之前添加了以下内容:
before_action :logged_in_user, only: [:show, :boli, :nolan]
现在我收到'用户呈现或重定向的消息'并且无法再登录。我没有遇到任何其他错误,有关如何克服此问题的任何想法?
以下是我的session
创建代码,session_helper
代码和user_controller
代码。
def create
user = User.find(1)
if user && user.authenticate(params[:session][:password])
redirect_to user
else
flash.now[:danger] = 'Invalid password'
render 'new'
end
end
module SessionsHelper
# Logs in the given user.
def log_in(user)
session[:user_id] = user.id
end
def current_user
if (user_id = session[:user_id])
@current_user ||= User.find_by(id: user_id)
elsif
user = User.find_by(id: user_id)
if user && user.authenticated?
log_in user
@current_user = user
end
end
end
def logged_in?
!current_user.nil?
end
def log_out
session.delete(:user_id)
@current_user = nil
end
end
用户控制器代码:
def logged_in_user
unless logged_in?
flash[:danger] = "Please log in."
redirect_to login_url
end
end
def correct_user
@user = User.find(params[:id])
redirect_to(root_url) unless @user == current_user
end
def log_in(user)
session[:user_id] = User.find(1)
end