简体   繁体   中英

Why can't I resolve the error ' WelcomeController # index is missing a template for this request format and variant. '?

My ruby version is 3.0.0, my mysql version is 5.7, my mysql2 version is 0.5.3, my bundler version is 2.2.3, my rails version is 5.0.7.2, and my Xcode version is 12.5. I use macOS Big Sur(version 11.2.2.) and Atom of the text editor.

I'm under production of the application called 'food-board'.

When I executed the command rails s , I got the following error code in the terminal.

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: []):

The above error code says "There is no template in the #index method of WelcomeController",

so I executed the following command to add routes, controllers, and views.

$ 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

The following two files( app/controllers/welcome_controller.rbapp/views/welcome/index.html.erb ) was added to the food-board folder after executing the command $ rails g controller Welcome index --no-helper --no-assets .

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

The following code has been added to the file app/controllers/welcome_controller.rb after executing the command $ rails g controller Welcome index --no-helper --no-assets .

class WelcomeController < ApplicationController
  def index
  end
end

The following code has been added to the file config/route.rb after executing the command $ rails g controller Welcome index --no-helper --no-assets .

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

The following code has been added to the file app/views/welcome/index.html.erb after executing the command $ rails g controller Welcome index --no-helper --no-assets .

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

After executing the command $ rails g controller Welcome index --no-helper --no-assets , I edited the file config/route.rb as follows.

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

The result of the execution of the command rails routes is as follows.

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

When I executed the command rails s again、 I got the following error code in the terminal.

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: []):

Would I like you to tell me why I can't resolve the following error?

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

First of all Ruby 3.0.0 does not support this version of rails. So you should install ruby 2.7.3 and then try as I check all other code there is no issue in them, it is only your ruby version.

Please make sure you check your Gemfile it may have mentioned about your ruby version and also if any .ruby_versin file in your root that may have ruby version.

When you go in your root folder make sure you are running ruby 2.7.3 version by following command

ruby --version

then run your server using

rails s

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM