Marchan4
@Marchan4 (Marchan4)

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

WelcomeController#index is missing a template (...)というエラーを解決できない理由を教えてください。

rubyのバージョンは3.0.0、 mysqlのバージョンは5.7、 mysql2のバージョンは0.5.3、
bundlerバージョンは2.2.3、 railsのバージョンは5.0.7.2、
Xcodeのバージョンは12.5です。
使用機器macOS Big Sur バージョン11.2.2です。使用中のテキストエディタはAtomです。

現在food-boardというアプリを製作中です。

rails sコマンドを実行致したら、ターミナルに以下のエラーコードが出ました。

Processing by WelcomeController#index as HTML
Completed 406 Not Acceptable in 35ms (ActiveRecord: 0.0ms)



ActionController::UnknownFormat (WelcomeController#index is missing a template for this request format and variant.

request.formats: ["text/html"]
request.variant: []):

上記のエラーコードは「WelcomeControllerの#indexメソッドにテンプレートがない」と言っておるため、

ルーティングの追加、コントローラーの追加、ビューの追加のために以下のコマンドを実行致しました。

$ rails g controller Welcome index --no-helper --no-assets

      create  app/controllers/welcome_controller.rb
       route  get 'welcome/index'
      invoke  erb
      create    app/views/welcome
      create    app/views/welcome/index.html.erb

food-boardフォルダには以下の2つのファイル
(app/controllers/welcome_controller.rb
app/views/welcome/index.html.erb)
が追加されました。

food-board
  └app
    └controllers
      └concerns
      └application_controller.rb
      └welcome_controller.rb
    └views
      └layouts
      └welcome
        └index.html.erb
  └config
    └routes.rb

app/controllers/welcome_controller.rbには以下のコードが追加されました。

class WelcomeController < ApplicationController
  def index
  end
end

config/route.rbには以下のコードが追加されました。

Rails.application.routes.draw do
  get 'welcome/index'
  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end

app/views/welcome/index.html.erbには以下のコードが追加されました。

<h1>Welcome#index</h1>
<p>Find me in app/views/welcome/index.html.erb</p>

config/route.rbを以下のように編集致しました。

Rails.application.routes.draw do
  root "welcome#index"
  get "welcome/index"
  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end

rails routesコマンドの実行結果が以下の通りです。

$ rails routes

       Prefix Verb URI Pattern              Controller#Action
         root GET  /                        welcome#index
welcome_index GET  /welcome/index(.:format) welcome#index

rails sコマンドを再び実行致したら、ターミナルに以下のエラーコードが出ました。

Processing by WelcomeController#index as HTML
Completed 406 Not Acceptable in 35ms (ActiveRecord: 0.0ms)



ActionController::UnknownFormat (WelcomeController#index is missing a template for this request format and variant.

request.formats: ["text/html"]
request.variant: []):

下記のエラー

ActionController::UnknownFormat (WelcomeController#index is missing a template for this request format and variant.

を解決できない理由を教えていただいてもよろしいでしょうか。

0

1Answer

Rails::WelcomeController は開発環境で http://localhost:3000 にアクセスしたとき表示される初期画面のコントローラです。 Rails が組み込みで用意してあるコントローラであり、テンプレートも存在するはずなので、 missing a template エラーになる理由はよく分かりません。

ともかく、このコントローラが使われるということは config/routes.rbget '/' のルーティングを設定していないのでは。

get '/', to: 'welcome#index'

と書けば app/controllers/welcome_controller.rb が使われると思います。

0Like

Your answer might help someone💌