未初始化的常量SessionsController ROR rails 4

时间:2015-07-08 17:30:34

标签: authentication ruby-on-rails-4 routes

嗨,我是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

我做错了吗?需要一些帮助。谢谢。

1 个答案:

答案 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} }