Step 1.4 CarsController#index is missing a template for request formats: text/html


#1

So I’m working on my first Ruby on Rails application and everything has gone pretty smoothly with some troubleshooting until now. I am able to launch $ rails server and then when I navigate to http://localhost:3000/cars I get the error message

No template for interactive request
CarsController#index is missing a template for request formats: text/html

NOTE!
Unless told otherwise, Rails expects an action to render a template with the same name,
contained in a folder named after its controller. If this controller is an API responding with 204 (No Content),
which does not require a template, then this error will occur when trying to access it via browser,
since we expect an HTML template to be rendered for such requests. If that’s the case, carry on.

I’ve tried some different things but nothing seems to resolve this. Any ideas?


#2

Sounds like the scaffold command you ran didn’t work properly.

In the app/controllers directory is there a cars_controller.rb file?


#3

Yes there is


#4

What’s in the contents of that cars_controller file?


#5
class CarsController < ApplicationController
before_action :set_car, only: [:show, :edit, :update, :destroy]

# GET /cars
# GET /cars.json
def index
 @cars = Car.all
end

# GET /cars/1
# GET /cars/1.json
def show
end

# GET /cars/new
def new
 @car = Car.new
end

# GET /cars/1/edit
def edit
end

# POST /cars
# POST /cars.json
def create
 @car = Car.new(car_params)

 respond_to do |format|
   if @car.save
     format.html { redirect_to @car, notice: 'Car was successfully created.' }
     format.json { render :show, status: :created, location: @car }
   else
     format.html { render :new }
     format.json { render json: @car.errors, status: :unprocessable_entity }
   end
 end
end

# PATCH/PUT /cars/1
# PATCH/PUT /cars/1.json
def update
 respond_to do |format|
   if @car.update(car_params)
     format.html { redirect_to @car, notice: 'Car was successfully updated.' }
     format.json { render :show, status: :ok, location: @car }
   else
     format.html { render :edit }
     format.json { render json: @car.errors, status: :unprocessable_entity }
   end
 end
end

# DELETE /cars/1
# DELETE /cars/1.json
def destroy
 @car.destroy
 respond_to do |format|
   format.html { redirect_to cars_url, notice: 'Car was successfully destroyed.' }
   format.json { head :no_content }
 end
end

private
 # Use callbacks to share common setup or constraints between actions.
 def set_car
   @car = Car.find(params[:id])
 end

 # Never trust parameters from the scary internet, only allow the white list through.
 def car_params
   params.require(:car).permit(:make, :model, :year)
 end
end

#6

One last question. In the app/views/cars directory is there a file called index.html.erb


#7

Yes I see it there

<p id="notice"><%= notice %></p>

<h1>Cars</h1>

<table>
  <thead>
    <tr>
      <th>Make</th>
      <th>Model</th>
      <th>Year</th>
      <th colspan="3"></th>
    </tr>
  </thead>

  <tbody>
    <% @cars.each do |car| %>
      <tr>
        <td><%= car.make %></td>
        <td><%= car.model %></td>
        <td><%= car.year %></td>
        <td><%= link_to 'Show', car %></td>
        <td><%= link_to 'Edit', edit_car_path(car) %></td>
        <td><%= link_to 'Destroy', car, method: :delete, data: { confirm: 'Are you sure?' } %></td>
      </tr>
    <% end %>
  </tbody>
</table>

<br>

<%= link_to 'New Car', new_car_path %>

#8

Try restarting the rails server.


#9

I have a few times, it causes the same error every time. I have also restarted my computer in case some elements weren’t installed properly as well as deleted the app and started from the beginning of the instructions. Running Ruby ruby 2.6.3p62 (2019-04-16 revision 67580) and rails 6.0.0


#10

This is a difficult one to debug.

Can you show me a screenshot of your terminal when you try to open localhost in your browser?


#11

Thanks for bearing with me here!


#12

No problem. Can you show me a screenshot of your rails app directory in the views/cars folder. I just want to see what got created.


#13

This is what I’ve got in there


#14

Man this is really weird as everything looks normal.

What are the contents of the config/routes.rb file?


#15
Rails.application.routes.draw do
  resources :cars
  # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
end

#16

Ok I may be onto something here.

what’s the output of running cat /etc/hosts

It should be run from the home directory which you’ll be in when you first open your terminal.


#17
##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting.  Do not change this entry.
##
127.0.0.1	localhost
255.255.255.255	broadcasthost
::1             localhost

#18

Try starting your rails server with bundle exec rails s -b localhost -p 3000


#19

No luck! I get the same error


#20

Ok can you try starting the rails server normally with rails server and then in your browser navigate to tcp://localhost:3000