質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

1回答

1103閲覧

Routing Error uninitialized constant Usersを解消したいです。(Rails)

chach

総合スコア0

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

0グッド

0クリップ

投稿2022/12/16 23:39

編集2022/12/17 05:27

現在、Railsにdeviseを導入し、複数モデルを作成しています。
テーブル:worker、salonの2つで作成中。

ローカル環境でsign_inの画面に直接アクセスすると、どちらも
ActionController::RoutingError (uninitialized constant Salons)
ActionController::RoutingError (uninitialized constant Workers):
のエラーが出ます。

上記エラー解消方法をご教示いただきたいです。

発生している問題・エラーメッセージ

Image from Gyazo

該当のソースコード

config/routes.rb

1Rails.application.routes.draw do 2 devise_for :salons, controllers: { 3 sessions: 'salons/sessions', 4 passwords: 'salons/passwords', 5 registrations: 'salons/registrations' 6 } 7 devise_for :workers, controllers: { 8 sessions: 'workers/sessions', 9 passwords: 'workers/passwords', 10 registrations: 'workers/registrations' 11 } 12 root to: 'home#index' 13end

app/controllers/salons_controller.rb

1class SalonsController < ApplicationController 2end

app/controllers/workers_controller.rb

1class WorkersController < ApplicationController 2end

app/models/salon.rb

1class Salon < ApplicationRecord 2 # Include default devise modules. Others available are: 3 # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable 4 devise :database_authenticatable, :registerable, 5 :recoverable, :rememberable, :validatable 6 7 validates :prefecture_id, numericality: { other_than: 1, message: "can't be blank" } 8 9 # パスワード 10 PASSWORD_REGEX = /\A(?=.*?[a-z])(?=.*?\d)[a-z\d]+\z/i.freeze 11 validates :password, format: { with: PASSWORD_REGEX, message: 'には6文字以上の半角英数字を使用して設定してください' }, allow_blank: true 12 13 14 extend ActiveHash::Associations::ActiveRecordExtensions 15 belongs_to :prefecture 16end

app/models/worker.rb

1class Worker < ApplicationRecord 2 # Include default devise modules. Others available are: 3 # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable 4 devise :database_authenticatable, :registerable, 5 :recoverable, :rememberable, :validatable 6 7 # ニックネーム 8 validates :nickname, presence: true 9 10 # パスワード 11 PASSWORD_REGEX = /\A(?=.*?[a-z])(?=.*?\d)[a-z\d]+\z/i.freeze 12 validates :password, format: { with: PASSWORD_REGEX, message: 'には6文字以上の半角英数字を使用して設定してください' }, allow_blank: true 13 14end

db/migrate/202212....._devise_create_workers.rb

1class DeviseCreateWorkers < ActiveRecord::Migration[6.0] 2 def change 3 create_table :workers do |t| 4 ## Database authenticatable 5 t.string :worker_email, null: false 6 t.string :encrypted_password, null: false 7 t.string :nickname, null: false 8 9 10 ## Recoverable 11 t.string :reset_password_token 12 t.datetime :reset_password_sent_at 13 14 ## Rememberable 15 t.datetime :remember_created_at 16 17 ## Trackable 18 # t.integer :sign_in_count, default: 0, null: false 19 # t.datetime :current_sign_in_at 20 # t.datetime :last_sign_in_at 21 # t.string :current_sign_in_ip 22 # t.string :last_sign_in_ip 23 24 ## Confirmable 25 # t.string :confirmation_token 26 # t.datetime :confirmed_at 27 # t.datetime :confirmation_sent_at 28 # t.string :unconfirmed_email # Only if using reconfirmable 29 30 ## Lockable 31 # t.integer :failed_attempts, default: 0, null: false # Only if lock strategy is :failed_attempts 32 # t.string :unlock_token # Only if unlock strategy is :email or :both 33 # t.datetime :locked_at 34 35 36 t.timestamps null: false 37 end 38 39 add_index :workers, :worker_email, unique: true 40 add_index :workers, :reset_password_token, unique: true 41 # add_index :workers, :confirmation_token, unique: true 42 # add_index :workers, :unlock_token, unique: true 43 end 44end

db/migrate/202212....._devise_create_salons.rb

1class DeviseCreateSalons < ActiveRecord::Migration[6.0] 2 def change 3 create_table :salons do |t| 4 ## Database authenticatable 5 t.string :salon_email, null: false 6 t.string :encrypted_password, null: false 7 t.string :store_name, null: false 8 t.string :corporate_name, null: false 9 t.string :postal_code, null: false 10 t.integer :prefecture_id, null: false 11 t.string :city, null: false 12 t.string :street_number, null: false 13 t.string :building_name 14 t.date :established, null: false 15 t.integer :offices, null: false 16 17 18 ## Recoverable 19 t.string :reset_password_token 20 t.datetime :reset_password_sent_at 21 22 ## Rememberable 23 t.datetime :remember_created_at 24 25 ## Trackable 26 # t.integer :sign_in_count, default: 0, null: false 27 # t.datetime :current_sign_in_at 28 # t.datetime :last_sign_in_at 29 # t.string :current_sign_in_ip 30 # t.string :last_sign_in_ip 31 32 ## Confirmable 33 # t.string :confirmation_token 34 # t.datetime :confirmed_at 35 # t.datetime :confirmation_sent_at 36 # t.string :unconfirmed_email # Only if using reconfirmable 37 38 ## Lockable 39 # t.integer :failed_attempts, default: 0, null: false # Only if lock strategy is :failed_attempts 40 # t.string :unlock_token # Only if unlock strategy is :email or :both 41 # t.datetime :locked_at 42 43 44 t.timestamps null: false 45 end 46 47 add_index :salons, :salon_email, unique: true 48 add_index :salons, :reset_password_token, unique: true 49 # add_index :salons, :confirmation_token, unique: true 50 # add_index :salons, :unlock_token, unique: true 51 end 52end

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

yuma.inaura

2022/12/17 06:28

uninitialized constant Users? uninitialized constant Workers?
chach

2022/12/17 11:51

uninitialized constant Workers です!
yuma.inaura

2022/12/17 11:53

タイトルがUsersのままですね
guest

回答1

0

推測を含みますが、エラーメッセージにあるモデルクラス(SalonsWorkers)の作成状況を確認するのが良いのではないかと思います(Controller Class - /salons_controller, workers_controllerのファイルは提示頂いていますが、モデルクラス(models)の状態はいかがでしょうか?

における、rails generate devise MODELrails db:migrateのステップのあたり、が関連するのではないかと思います。このあたりのファイルの状況や、設定操作の状況も提示いただけると、回答が集まりやすいのではないかと思います。

投稿2022/12/17 04:24

編集2022/12/17 04:28
knuser

総合スコア178

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問