繁体   English   中英

为什么我无法解决错误“WelcomeController # index is missing a template for this request format and variant。 '?

[英]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. 我使用 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

执行命令 $ rails g app/controllers/welcome_controller.rb app/views/welcome/index.html.erb $ 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

执行命令$ rails g controller Welcome index --no-helper --no-assets后,以下代码已添加到文件app/controllers/welcome_controller.rb中。

class WelcomeController < ApplicationController
  def index
  end
end

在执行命令$ rails g controller Welcome index --no-helper --no-assets后,以下代码已添加到文件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

在执行命令$ rails g controller Welcome index --no-helper --no-assets后,以下代码已添加到文件app/views/welcome/index.html.erb中。

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

执行命令$ rails g controller Welcome index --no-helper --no-assets后,我将文件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.

首先Ruby 3.0.0不支持这个版本的rails。 所以你应该安装ruby 2.7.3然后尝试检查所有其他代码,它们没有问题,它只是你的 ruby 版本。

请确保检查您的Gemfile ,它可能提到了您的 ruby 版本,以及您的根目录中是否有任何可能具有 ruby 版本的.ruby_versin文件。

当您在根文件夹中使用 go 时,请通过以下命令确保您正在运行ruby 2.7.3版本

ruby --version

然后使用运行您的服务器

rails s

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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