如何解决为什么我无法解决错误“ WelcomeController # index 缺少此请求格式和变体的模板 '?
我的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
以下两个文件(app/controllers/welcome_controller.rb
、
app/views/welcome/index.html.erb
) 在执行命令 $ rails g controller Welcome index --no-helper --no-assets
后被添加到 food-board 文件夹中。
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
后,在文件 $ rails g controller Welcome index --no-helper --no-assets
中添加了以下代码。
class WelcomeController < ApplicationController
def index
end
end
执行命令 config/route.rb
后,在文件 $ 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
执行命令 app/views/welcome/index.html.erb
后,在文件 $ rails g controller Welcome index --no-helper --no-assets
中添加了以下代码。
<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_versin
文件可能包含 ruby 版本。
当您进入根文件夹时,请确保按照以下命令运行 ruby 2.7.3
版本
ruby --version
然后使用
运行您的服务器rails s
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。