Authlogicで任意のモデル名で認証機能を実現したい
現在、Ruby on Railsを用いて、認証機能を持った社員管理のアプリを構築しています。
gem Authlogic
を用いて認証機能の実装を行っていますが、
モデル名に社員を示す、「Employee」を指定した場合、ログイン画面表示時にName Error
が発生し、
うまく実装ができず、原因もつかめていません。
Authlogicのチュートリアル等を見ると、Userモデルで実装を行っている記事が多く見受けられますが、
Userモデル以外での実装は可能でしょうか?
また可能な場合、原因をご教授いただけますでしょうか。
初投稿なため、不足している情報等がありましたら合わせてご指摘いただければ幸いです。
よろしくお願いします。
エラーの内容
NameError in UserSessionsController#new uninitialized constant User
ソースコード
- app/model/employee.rb
Ruby
1class Employee < ApplicationRecord 2 acts_as_authentic 3end
- app/model/user_session.rb
Ruby
1class UserSession < Authlogic::Session::Base 2end
- app/controllers/user_sessions_controller.rb
Ruby
1class UserSessionsController < ApplicationController 2 skip_before_action :require_login, only: [:new, :create] 3 4 # GET /login 5 def new 6 @user_session = UserSession.new 7 end 8 9 # POST /user_session 10 def create 11 @user_session = UserSession.new(user_session_params.to_h) 12 13 if @user_session.save 14 return redirect_to login_url 15 end 16 end 17 18 # DELETE /logout 19 def destroy 20 current_user_session.destroy 21 redirect_to login_url 22 end 23 24 private 25 def user_session_params 26 params.require(:user_session).permit(:email, :password) 27 end 28end
- employeeのマイグレーションファイル
Ruby
1class CreateEmployees < ActiveRecord::Migration[5.2] 2 def change 3 create_table :employees do |t| 4 # Employee information 5 t.string :email, null: false, comment: 'メールアドレス' 6 t.integer :number, unique: true, null: false, comment: '社員番号' 7 t.string :name, null: false, comment: '社員名' 8 t.string :department, comment: '所属部署' 9 10 # Authlogic 11 t.string :crypted_password, null: false 12 t.string :password_salt, null: false 13 t.string :persistence_token, null: false 14 15 t.timestamps 16 end 17 end 18end
バージョン情報
Ruby: 2.5.3
Ruby on Rails: 5.2.3
Authlogic: 5.0.2

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。