简体   繁体   English

Ruby on Rails 资源#index 缺少请求格式的模板:text/html

[英]Ruby on Rails resources #index is missing a template for request formats: text/html

while trying to familiarize with using resources for planning routes, I encountered a weird error:在尝试熟悉使用资源来规划路线时,我遇到了一个奇怪的错误:

No template for interactive request没有交互式请求的模板

ShoppersController#index is missing a template for request formats: text/html ShoppersController#index 缺少请求格式的模板:text/html 错误信息

Here are the routes mapping这是路线映射创建的路线

routes.rb路线.rb

Rails.application.routes.draw do
 resources :shoppers 
end

shoppers_controller.rb购物者控制器.rb

class ShoppersController < ApplicationController

  def index 
  end

  def create
    @shopper = Shopper.new
  end

end

shoppers.html.erb购物者.html.erb

<h1>Welcome Shoppers</h1>

Does anyone know how to solve this?有谁知道如何解决这个问题?

Thanks for all the feedbacks you share.感谢您分享的所有反馈。

It's because the name of your view is wrong.这是因为您的视图名称错误。 As the error you're getting says: 'Rails expects an action to render a template with the same name contained in a folder named after its controller'正如您收到的错误所说: 'Rails 需要一个操作来呈现一个具有相同名称的模板,该模板包含在以其控制器命名的文件夹中'

So in your case, the structure needs to be:所以在你的情况下,结构需要是:

  • app应用程序
    • controllers控制器
      • shoppers_controller.rb购物者控制器.rb
    • views意见
      • shoppers购物者
        • index.html.erb索引.html.erb
        • new.html.erb新的.html.erb

Reference: https://guides.rubyonrails.org/layouts_and_rendering.html#rendering-by-default-convention-over-configuration-in-action参考: https://guides.rubyonrails.org/layouts_and_rendering.html#rendering-by-default-convention-over-configuration-in-action

Create a view with the name of the action in the controller!在控制器中创建一个带有动作名称的视图!

暂无
暂无

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

相关问题 为什么我在 Rails 上收到此 Ruby 错误?:#index 缺少请求格式的模板:text/html - Why am I getting this Ruby on Rails Error?: #index is missing a template for request formats: text/html 如何修复索引缺少请求格式的模板:text/html - How to fix index is missing a template for request formats: text/html 控制器缺少请求格式的模板:text/html - Controller is missing a template for request formats: text/html 缺少请求格式的模板:text / html - is missing a template for request formats: text/html Api::V1::CompaniesController#index 缺少请求格式的模板:text/html - Api::V1::CompaniesController#index is missing a template for request formats: text/html 如何修复 ActionController::MissingExactTemplate: EventsController#index 缺少请求格式的模板:text/html - How do I fix ActionController::MissingExactTemplate: EventsController#index is missing a template for request formats: text/html HomeController#index 缺少请求格式的模板:text/html,即使它存在 - HomeController#index is missing a template for request formats: text/html even though it exists 如何修复 PagesController#home 缺少请求格式的模板:text/html - How to fix PagesController#home is missing a template for request formats: text/html 缺少Ruby on Rails项目上的模板博客/索引 - Missing template blogs/index on Ruby on Rails project ParksController#create 缺少此请求格式和变体的模板。 request.formats: [“text/html”] request.variant: [] - ParksController#create is missing a template for this request format and variant. request.formats: [“text/html”] request.variant: []
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM