尝试设置Logout操作时,我在Sessions_Controller中创建了Session#destroy方法。以下是完整的控制器:
class SessionsController < ApplicationController
before_action :set_session, only: [:show, :edit, :update, :destroy]
helper_method :current_user
def new
end
def create
@user = User.find_by_email(params[:session][:email])
if @user && @user.authenticate(params[:session][:password])
session[:user_id] = @user.id
redirect_to '/tasks'
else
redirect_to 'login'
end
end
def destroy
log_out
redirect_to '/login'
end
log_out在sessions_helper.rb中显示如下:
def log_out
session.delete(:user_id)
@current_user = nil
end
尝试使用我已设置的注销按钮或导航到localhost:3000 / logout时收到以下错误。
错误消息
我甚至无法找到代码来更仔细地查看它。有任何想法吗?
答案 0 :(得分:1)
class SessionsController < ApplicationController
# move this to ApplicationController instead of duplicating it all over your app
helper_method :current_user
def new
end
def create
@user = User.find_by_email(params[:session][:email])
if @user && @user.authenticate(params[:session][:password])
session[:user_id] = @user.id
redirect_to '/tasks'
else
redirect_to '/login'
end
end
def destroy
log_out
redirect_to '/login'
end
end
def log_out
reset_session
@current_user = nil
end