我有Rails 4.0.10。我正在关注Michael Hartl的Ruby on Rails教程,我正在尝试构建一个登录功能,但是当我按下登录按钮时出现以下错误:
Routing Error
uninitialized constant SessionController
我完全按照说明操作,所以我很困惑为什么我会收到错误。我做错了什么?
我的会话控制器:
class SessionsController < ApplicationController
def new
end
def create
user = User.find_by(id: params[session][:id])
if user && user.authenticate(params[:session][:password])
log_in user
redirect_to root_path
else
flash.now[:danger] = 'Invalid'
render 'new'
end
end
def destroy
end
end
路线:
Website::Application.routes.draw do
get 'login' => 'sessions#new'
post 'login' => 'session#create'
delete 'logout' => 'sessions#destroy'
get "users/new"
root 'home_page#home'
end
会话/新视图:
<div id= "admin-sign-in">
<%= form_for(:session, url: login_path) do |f| %>
<%= f.label :id %>
<%= f.text_field :id %>
<%= f.label :password %>
<%= f.password_field :password %>
<%= f.submit "Log in", class: "btn btn-primary" %>
<% end %>
</div>
Sessions Helper:
module SessionsHelper
def log_in(user)
session[:user_id] = user.id
end
end
用户模型:
class User < ActiveRecord::Base
has_secure_password
end
用户控制器:
class UsersController < ApplicationController
def new
@user = User.new
@users = User.all
end
def create
@user = User.new(user_params)
if @user.save
render :action => "crop"
else
render 'new'
end
end
private
def user_params
params.require(:user).permit(:password, :password_confirmation)
end
end
的ApplicationController:
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
include SessionsHelper
end
答案 0 :(得分:10)
而不是:
post 'login' => 'session#create'
使用复数:
post 'login' => 'sessions#create'