我正在关注this Ruby Railcasts episode以获得一些简单的身份验证,当我导航到登录页面时,我收到错误uninitialized constant Sessions
。我检查了我的课程,名字是正确的,这似乎是大多数人报告的问题。知道这里可能会发生什么吗?
我的控制器文件的名称是sessions_controller.rb
,代码如下:
class SessionsController < ApplicationController
def new
end
def create
user = User.authenticate(params[:email], params[:password])
if user
session[:user_id] = user.id
redirect_to root_url, :notice => 'Logged In!'
else
flash.now.alert = 'Invalid email or password'
render 'new'
end
end
def destroy
session[:user_id] = nil
redirect_to root_url, :notice => 'Logged Out!'
end
end
routes.rb文件:
Albumtracker::Application.routes.draw do
get "login" => "sessions/new", :as => "login"
get "logout" => "sessions/destroy", :as => "logout"
get "signup" => "users/new", :as => "sign_up"
root :to => 'users#new'
resources :users
resources :sessions
get "pages/index"
end
会话/新视图文件:
<h1>Log in</h1>
<%= form_tag sessions_path do %>
<p>
<%= label_tag :email %><br />
<%= text_field_tag :email, params[:email] %>
</p>
<p>
<%= label_tag :password %><br />
<%= password_field_tag :password %>
</p>
<p class="button"><%= submit_tag "Log in" %></p>
<% end %>
答案 0 :(得分:4)
您的路线中存在拼写错误。指定控制器和操作时,/
应为#
。您的路线应如下所示:
get "login" => "sessions#new", :as => "login"
get "logout" => "sessions#destroy", :as => "logout"
get "signup" => "users#new", :as => "sign_up"
在路线中使用/
时,前一部分与命名空间匹配。因此,从"sessions/new"
路由器尝试找到名为Sessions::NewController
的控制器。由于您的应用中不存在名称空间Sessions
,因此您获得了未初始化的常量错误。