我是Ruby on Rails的新手,我目前正在尝试创建用户登录页面。要做到这一点,我使用Sessions。每当我打开登录页面时,它都会加载,但当我尝试输入我认识的用户的登录信息时,我会收到错误
Unknown action
The action 'create' could not be found for SessionsController
我的路线文件如下所示。另外作为一个注释,我在这里使用教师作为用户类。
MyApp::Application.routes.draw do
root :to => 'home#index'
get 'teachers' => 'teachers#all'
get 'profile' => 'teachers#show'
get 'signup' => 'teachers#new'
get 'login' => 'sessions#new'
post 'login' => 'sessions#create'
delete 'logout' => 'sessions#destroy'
resources :teachers
end
和会话控制器
class SessionsController < ApplicationController
module SessionsHelper
def new
@session=Session.new
end
def log_in(teacher)
session[:teacher_id] = teacher.id
end
def create
teacher = Teacher.authenticate(params[:session][:email],
params[:session][:password])
if teacher.nil?
flash.now[:error] = "Invalid email/password combination."
@title = "Sign in"
render 'new'
else
sign_in teacher
redirect_to teacher
end
end
def remember(teacher)
teacher.remember
cookies.permanent.signed[:teacher_id] = teacher.id
cookies.permanent[:remember_token] = teacher.remember_token
end
def current_teacher
if (teacher_id = session[:teacher_id])
@current_teacher ||= Teacher.find_by(id: teacher_id)
elsif (teacher_id = cookies.signed[:teacher_id])
teacher = Teacher.find_by(id: teacher_id)
if teacher && teacher.authenticated?(cookies[:remember_token])
log_in teacher
@current_teacher = teacher
end
end
end
def logged_in?
!current_teacher.nil?
end
def log_out
session.delete(:teacher_id)
@current_teacher = nil
end
def destroy
log_out if logged_in?
redirect_to root_url
end
end
end
有什么方法可以解决这个问题吗?