В моем проекте Rails я запустил:
rails generate scaffold car
, который успешно создал все необходимые файлы и каталоги для приложения.Это включает в себя стандартный файл макета application.html.erb
, а также каталог app / views / cars, который включает в себя больше стандартных файлов html.erb
(index, show, ETC.)
Кроме того, мой маршрут автомобилей находится вместо в config/routes.rb
.
Моя следующая команда - это сервер rails, что также успешно.Когда я открываю свой браузер и захожу на localhost:3000
, страница приветствия ruby on rails появляется как обычно.Я ожидаю, что когда я перейду на localhost:3000/cars
, я увижу html, отображаемый с app/views/cars/index.html.erb
.Если нет, я, по крайней мере, ожидаю, что по умолчанию он будет application.html.erb
.
Вместо этого я неоднократно получаю следующее:
>ActionController::UnknownFormat in CarsController#index
>CarsController#index is missing a template for this request format and variant. request.formats: ["text/html"] request.variant: []
Мой класс контроллера выглядит следующим образом:
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.fetch(:car, {})
end
end
Буду очень признателен за понимание этого вопроса.Я следовал всем советам, которые я видел в интернете относительно макетов, контроллеров и представлений, и мне не повезло.
Заранее спасибо!