Search code examples
ruby-on-railsrubyruby-on-rails-5resources

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 error message

Here are the routes mapping routes created

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.


Solution

  • 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