【问题标题】:get request not working in ruby on rails - 6.1.4.1获取请求在 ruby​​ on rails 中不起作用 - 6.1.4.1
【发布时间】:2021-11-10 15:51:45
【问题描述】:

Rail 版本详细信息

ruby 3.0.2p107 (2021-07-07 revision 0db68f0233) [x64-mingw32]

到目前为止我搜索了什么? - 以下都没有帮助我找出问题的根本原因

missing a template for this request format and variant

https://www.reddit.com/r/rails/comments/kkh4zg/why_am_i_getting_this_error/

https://www.ruby-forum.com/t/action-controller-is-missing-a-template-for-request-formats/255027

使用语法:rails new practise1 创建应用程序后,我在架构中添加了以下代码,

在 app/controller 中,创建了一个新的控制器 about_controller.rb

class AboutController < ApplicationController
    def index
    end
end

内部视图。创建了一个关于的新文件夹并添加了一个文件 index.html.erb

然后,最后在 config/routes.rb 文件中添加下面的代码,

Rails.application.routes.draw do
  get "about", to: "about#index"
end

错误详情

【问题讨论】:

  • 你的项目中app/views/about/有html文件吗?
  • 内部视图。创建了一个关于的新文件夹并添加了一个文件 index.html.erb
  • 你从rake routesrails routes得到什么?
  • 有一大堆路线,给你:i.stack.imgur.com/14Vv2.png
  • 看起来不错。在您的rails server 中,它是否给出了系统找不到文件的特定路径? (可能是您的目录必须是“abouts”而不是“about”)。我还假设您正在“开发”模式下工作,而不是 production?

标签: ruby-on-rails


【解决方案1】:

您的错误是说您的模板丢失。

当您在控制器About 中创建方法index 时,您应在以下文件中创建模板:app/views/about/index.html.erb

您可以看到 Rails 入门的所有步骤: https://guides.rubyonrails.org/getting_started.html#say-hello-rails


在 Windows 上,使用 PowerShell 时,您的路径可能会出现问题。 Rails 区分大小写,因此您需要注意启动rails s 的路径。

你可以这样看:

❯ echo $PWD
/Users/username/rails/myRailsApp
❯ rails c
Running via Spring preloader in process 24579
Loading development environment (Rails 6.1.4.1)
irb(main):001:0> ActionView::LookupContext.fallbacks
=> 
[#<ActionView::FallbackFileSystemResolver:0x00007ff320b86918
  @cache=#<ActionView::Resolver::Cache:0x00007ff320b861e8 keys=0 queries=0>,
  @path="/Users/username/rails/myRailsApp",
  @path_parser=#<ActionView::Resolver::PathParser:0x00007ff320b86350>,
  @pattern=":prefix/:action{.:locale,}{.:formats,}{+:variants,}{.:handlers,}",
  @unbound_templates=#<Concurrent::Map:0x00007ff320b86710 entries=0 default_proc=nil>>,
 #<ActionView::FallbackFileSystemResolver:0x00007ff320b858b0
  @cache=#<ActionView::Resolver::Cache:0x00007ff320b85748 keys=0 queries=0>,
  @path="/",
  @path_parser=#<ActionView::Resolver::PathParser:0x00007ff320b85770>,
  @pattern=":prefix/:action{.:locale,}{.:formats,}{+:variants,}{.:handlers,}",
  @unbound_templates=#<Concurrent::Map:0x00007ff320b85888 entries=0 default_proc=nil>>]

@path 正在获取您的 shell 给定的环境 PWD 值。 Rails 将使用它来解析您的模板文件。

https://discuss.rubyonrails.org/t/getting-started-with-rails-no-template-for-interactive-request/76162/2

【讨论】:

  • 我的 index.html.erb 可以位于 views/about 文件夹中,具有以下内容:&lt;h1&gt;Hello, Rails!&lt;/h1&gt; 这是否仅适用于此内容?
  • 内容够了。但我认为您应该确认您在正确的位置创建了模板。请尝试ls app/views/about/index.html.erb。否则这似乎很奇怪..
  • i.stack.imgur.com/9ECLq.png 这是路线详情。这是文件资源管理器:i.stack.imgur.com/pj9XY.png我错过了什么吗?
  • 重新加载页面时控制台显示了什么?
  • i.stack.imgur.com/gM8Hz.png,如果您需要更多信息或团队查看器,请告诉我
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多