嗨,我是Ruby on rails的初学者。我正在尝试一些实验。我正在尝试实现用户登录。我没有使用任何宝石。我尝试了以下方式:
get "log_out" => "sessions#destroy", :as => "log_out"
get "log_in" => "sessions#new", :as => "log_in"
get "sign_up" => "users#new", :as => "sign_up"
root :to => "users#new"
resources :users
resources :sessions
class SessionController < 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
当我试图点击http://localhost:3000/log_in
时显示
uninitialized constant SessionsController
routing error
我做错了吗?需要一些帮助。谢谢。
答案 0 :(得分:2)
您SessionController
没有SessionsController
。将其更改为SessionsController
或修改routes
,如下所示。
get "log_out" => "session#destroy", :as => "log_out"
get "log_in" => "session#new", :as => "log_in"
get "sign_up" => "users#new", :as => "sign_up"
root :to => "users#new"
resources :users
resource :session
我建议将SessionController
更改为SessionsController
。
IMP注意:
将SessionController
更改为SessionsController
时,不要忘记将 文件名 更改为 {{1} } 强>