1
0

More than 1 year has passed since last update.

Routing Error(uninitialized constant HomesController Object.const_get...)の解決について

Posted at

概要

railsのタグ機能の練習がしたくて、railsの環境を構築しましたが、routingのエラーが起きたので解決の方法を記述します。

環境

rails 7.0.4.2
ruby 3.1.2
AWS
cloud9
mac

エラーの意味

エラー文をdeeplで検索します。

uninitialized constant HomesController Object.const_get(camel_cased_word) ^^^^^^^^^^ Did you mean? HomeController raise MissingController.new(error.message, error.name) ^^^^^

uninitialized constant HomesController Object.const_get(camel_cased_word) ^^^^ ってこと?HomeController raise MissingController.new(error.message, error.name) ^^^。

このやろう...

整理します。

uninitialized constant HomesController

未初期化の定数HomesController

HomeController raise Missing

HomeControllerが見つからない
HomeControllerが存在しない
HomeControllerがない場合

ん?Homecontrollerがない?
どういうことだ?と思って状況をもう一度整理します。

routing、controller、view

route.rb
Rails.application.routes.draw do
  root to: "homes#top"
  # Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html

  # Defines the root path route ("/")
  # root "articles#index"
end
home_controller.rb
class HomeController < ApplicationController
  def top
  end
end
top.html.erb
<h1>test</h1>

あ!

route.rb
Rails.application.routes.draw do
  root to: "homes#top"
  # Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html

  # Defines the root path route ("/")
  # root "articles#index"
end

root to: "homes#top"

この書き方だと、homesコントローラーのtopアクションを呼び出すよ!ってことなので、今回のhome_controllerとは合わない。
HomeController raise Missing

HomeControllerが見つからない
HomeControllerが存在しない
HomeControllerがない場合
や、そもそもの
uninitialized constant HomesController Object.const_get(camel_cased_word) ^^^^ ってこと?HomeController raise MissingController.new(error.message, error.name) ^^^。
は正しかったんだ・・・ごめんよ・・・

結論

そもそもエラー文をよく読めって話ですね。気をつけます。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0