【Ruby on Rails エラー】uninitialized constant HomeController

Ruby on Rails

Ruby on Railsで実装中にエラーが発生したので、メモしておきます。

スポンサーリンク

エラー状況

deviseを使ってログインを実装しようとしたところ、userモデルを作成して確認しようとしサーバーにアクセスしたところ上記のエラーが出ました。

uninitialized constant HomeController
Rails.root: /myapp

Application Trace | Framework Trace | Full Trace
Routes
Routes match in priority from top to bottom

Helper	HTTP Verb	Path	Controller#Action
Path / Url		
Path Match
root_path	GET	/	
home#index

refile_app_path		/attachments	
#

rails_service_blob_path	GET	/rails/active_storage/blobs/:signed_id/*filename(.:format)	
active_storage/blobs#show

rails_blob_representation_path	GET	/rails/active_storage/representations/:signed_blob_id/:variation_key/*filename(.:format)	
active_storage/representations#show

rails_disk_service_path	GET	/rails/active_storage/disk/:encoded_key/*filename(.:format)	
active_storage/disk#show

update_rails_disk_service_path	PUT	/rails/active_storage/disk/:encoded_token(.:format)	
active_storage/disk#update

rails_direct_uploads_path	POST	/rails/active_storage/direct_uploads(.:format)	
active_storage/direct_uploads#create

ルーティングを確認してみます。

% rails routes
                   Prefix Verb URI Pattern                                                                              Controller#Action
                     root GET  /                                                                                        home#index
               refile_app      /attachments                                                                             #<Refile::App app_file="/Users/user/.bundle/ruby/2.6.0/refile-46b4178654e6/lib/refile/app.rb">
       rails_service_blob GET  /rails/active_storage/blobs/:signed_id/*filename(.:format)                               active_storage/blobs#show
rails_blob_representation GET  /rails/active_storage/representations/:signed_blob_id/:variation_key/*filename(.:format) active_storage/representations#show
       rails_disk_service GET  /rails/active_storage/disk/:encoded_key/*filename(.:format)                              active_storage/disk#show
update_rails_disk_service PUT  /rails/active_storage/disk/:encoded_token(.:format)                                      active_storage/disk#update
     rails_direct_uploads POST /rails/active_storage/direct_uploads(.:format)                                           active_storage/direct_uploads#create

ログイン画面など何もないですね。。。

ソースをみているとコントローラーがなかったので、コントローラーを作成します。

% rails g devise:controllers users
      create  app/controllers/users/confirmations_controller.rb
      create  app/controllers/users/passwords_controller.rb
      create  app/controllers/users/registrations_controller.rb
      create  app/controllers/users/sessions_controller.rb
      create  app/controllers/users/unlocks_controller.rb
      create  app/controllers/users/omniauth_callbacks_controller.rb
===============================================================================

Some setup you must do manually if you haven't yet:

  Ensure you have overridden routes for generated controllers in your routes.rb.
  For example:

    Rails.application.routes.draw do
      devise_for :users, controllers: {
        sessions: 'users/sessions'
      }
    end

===============================================================================

指示通り、routes.rbにRails.application.routes.draw do。。。。を記載します。

再度ルーティングを確認しました。
ちゃんとルーティングできていそうですね。

% rails routes
                   Prefix Verb   URI Pattern                                                                              Controller#Action
                     root GET    /                                                                                        home#index
         new_user_session GET    /users/sign_in(.:format)                                                                 users/sessions#new
             user_session POST   /users/sign_in(.:format)                                                                 users/sessions#create
     destroy_user_session DELETE /users/sign_out(.:format)                                                                users/sessions#destroy
        new_user_password GET    /users/password/new(.:format)                                                            devise/passwords#new
       edit_user_password GET    /users/password/edit(.:format)                                                           devise/passwords#edit
            user_password PATCH  /users/password(.:format)                                                                devise/passwords#update
                          PUT    /users/password(.:format)                                                                devise/passwords#update
                          POST   /users/password(.:format)                                                                devise/passwords#create
 cancel_user_registration GET    /users/cancel(.:format)                                                                  devise/registrations#cancel
    new_user_registration GET    /users/sign_up(.:format)                                                                 devise/registrations#new
   edit_user_registration GET    /users/edit(.:format)                                                                    devise/registrations#edit
        user_registration PATCH  /users(.:format)                                                                         devise/registrations#update
                          PUT    /users(.:format)                                                                         devise/registrations#update
                          DELETE /users(.:format)                                                                         devise/registrations#destroy
                          POST   /users(.:format)                                                                         devise/registrations#create
               refile_app        /attachments                                                                             #<Refile::App app_file="/Users/user/.bundle/ruby/2.6.0/refile-46b4178654e6/lib/refile/app.rb">
       rails_service_blob GET    /rails/active_storage/blobs/:signed_id/*filename(.:format)                               active_storage/blobs#show
rails_blob_representation GET    /rails/active_storage/representations/:signed_blob_id/:variation_key/*filename(.:format) active_storage/representations#show
       rails_disk_service GET    /rails/active_storage/disk/:encoded_key/*filename(.:format)                              active_storage/disk#show
update_rails_disk_service PUT    /rails/active_storage/disk/:encoded_token(.:format)                                      active_storage/disk#update
     rails_direct_uploads POST   /rails/active_storage/direct_uploads(.:format)                                           active_storage/direct_uploads#create

でも今度も以下のようなエラーが出ました。

uninitialized constant HomeController
Rails.root: /myapp

Application Trace | Framework Trace | Full Trace
Routes
Routes match in priority from top to bottom

Helper	HTTP Verb	Path	Controller#Action
Path / Url		
Path Match
root_path	GET	/	
home#index

new_user_session_path	GET	/users/sign_in(.:format)	
users/sessions#new

user_session_path	POST	/users/sign_in(.:format)	
users/sessions#create

destroy_user_session_path	DELETE	/users/sign_out(.:format)	
users/sessions#destroy

new_user_password_path	GET	/users/password/new(.:format)	
devise/passwords#new

edit_user_password_path	GET	/users/password/edit(.:format)	
devise/passwords#edit

user_password_path	PATCH	/users/password(.:format)	
devise/passwords#update

PUT	/users/password(.:format)	
devise/passwords#update

POST	/users/password(.:format)	
devise/passwords#create

cancel_user_registration_path	GET	/users/cancel(.:format)	
devise/registrations#cancel

new_user_registration_path	GET	/users/sign_up(.:format)	
devise/registrations#new

edit_user_registration_path	GET	/users/edit(.:format)	
devise/registrations#edit

user_registration_path	PATCH	/users(.:format)	
devise/registrations#update

PUT	/users(.:format)	
devise/registrations#update

DELETE	/users(.:format)	
devise/registrations#destroy

POST	/users(.:format)	
devise/registrations#create

refile_app_path		/attachments	
#

rails_service_blob_path	GET	/rails/active_storage/blobs/:signed_id/*filename(.:format)	
active_storage/blobs#show

rails_blob_representation_path	GET	/rails/active_storage/representations/:signed_blob_id/:variation_key/*filename(.:format)	
active_storage/representations#show

rails_disk_service_path	GET	/rails/active_storage/disk/:encoded_key/*filename(.:format)	
active_storage/disk#show

update_rails_disk_service_path	PUT	/rails/active_storage/disk/:encoded_token(.:format)	
active_storage/disk#update

rails_direct_uploads_path	POST	/rails/active_storage/direct_uploads(.:format)	
active_storage/direct_uploads#create

ルーティングに何か足りてないみたいですね。

よくみるとHomeControllerないやんけって言われてますね。

あ。。。
そもそもHomeControllerを作っていなかったです。。。

今までlocalhost:3000に接続してました。。。

localhost:3000/users/sign_upに接続してみます。

Showing /myapp/app/views/layouts/application.html.erb where line #9 raised:

undefined method `javascript_pack_tag' for #<#<Class:0x000055d71c5e49f0>:0x00007f0a7cb6d5b8>
Did you mean?  javascript_path
               javascript_tag
Extracted source (around line #9):
7
8
9
10
11
12
              

    <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
    <%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %>
  </head>

  <body>

Rails.root: /myapp

Application Trace | Framework Trace | Full Trace
app/views/layouts/application.html.erb:9:in `_app_views_layouts_application_html_erb___3951240858885647082_69841509409880'

application.html.erbのjavascript_pack_tagがないって怒られました。

調べましたがよくわからないので、削除しました。

これを消した。
<%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %>

とりあえず画面が表示されました。

HomeControllerの作成

結局最初のエラーは解決していないので、HomeControllerを作って行きます。

以下のコマンドを実行します。

% rails g controller home index
      create  app/controllers/home_controller.rb
       route  get 'home/index'
      invoke  erb
      create    app/views/home
      create    app/views/home/index.html.erb
      invoke  test_unit
      create    test/controllers/home_controller_test.rb
      invoke  helper
      create    app/helpers/home_helper.rb
      invoke    test_unit
      invoke  assets
      invoke    js
      create      app/assets/javascripts/home.js
      invoke    css
      create      app/assets/stylesheets/home.css

これでエラーが出なくなりました。

フリーランスを目指すなら【テックパートナーズカレッジ】

未経験者向けWEBスクール【SHElikes】

コメント