简体   繁体   中英

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 错误信息

Here are the routes mapping创建的路线

routes.rb

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

shoppers_controller.rb

class ShoppersController < ApplicationController

  def index 
  end

  def create
    @shopper = Shopper.new
  end

end

shoppers.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'

So in your case, the structure needs to be:

  • app
    • controllers
      • shoppers_controller.rb
    • views
      • shoppers
        • index.html.erb
        • new.html.erb

Reference: 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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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