Ruby-on-rails 缺少此请求格式和变体的模板
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40009571/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
missing a template for this request format and variant
提问by codingdraculasbrain
I am new to Ruby on Rails and am trying to gain a strong understanding of how MVC works.
我是 Ruby on Rails 的新手,正在努力深入了解 MVC 的工作原理。
I did the following:
我做了以下事情:
rails new bubblesman
rails generate controller bubble
in my bubble controller I created a method as follows:
在我的气泡控制器中,我创建了一个方法,如下所示:
def available
puts "YEP!!!!!!"
end
I put the following in my routes file:
我将以下内容放在我的路由文件中:
'welcome' => 'bubble#available'
I navigate to http://localhost:3000/welcomeI get the below error:
我导航到http://localhost:3000/welcome我收到以下错误:
ActionController::UnknownFormat (BubbleController#available is missing a template for this request format and variant.
request.formats: ["text/html"]
request.variant: []
NOTE! For XHR/Ajax or API requests, this action would normally respond with 204 No Content: an empty white screen. Since you're loading it in a web browser, we assume that you expected to actually render a template, not… nothing, so we're showing an error to be extra-clear. If you expect 204 No Content, carry on. That's what you'll get from an XHR or API request. Give it a shot.):
what I also don't understand is if I put this in my helper controller instead of my main controller it all works fine.
我也不明白的是,如果我把它放在我的辅助控制器而不是我的主控制器中,它一切正常。
回答by Avir94
you need to create the available.html.erbfile within the views/bubble/directory. When the route takes you to that action, it also navigates you to that view, so if you put:
您需要available.html.erb在views/bubble/目录中创建文件。当路线将您带到该操作时,它还会将您导航到该视图,因此如果您输入:
<h2>YEP!!!!</h2>
as the only line in that file, it should return that to you on the webpage.
作为该文件中的唯一一行,它应该在网页上将其返回给您。
In the future, you could use rails g scaffold bubblesand that will create a majority of the files (MVC) and routes for you.
将来,您可以使用它rails g scaffold bubbles,这将为您创建大部分文件 (MVC) 和路由。

