【发布时间】:2020-03-19 09:23:18
【问题描述】:
我知道这个错误是什么,但我在我的代码中看不到错误。 控制器是复数,型号是单数,表名是复数。
访问索引出错:
NameError 在 /admin/custom_communities 未初始化常量 Admin::CustomCommunitiesController::CustomCommunity
生成的控制器:(文件:controllers/admin/custom_communities_controller.rb)
# frozen_string_literal: true
class Admin::CustomCommunitiesController < Admin::BaseController
before_action :set_custom_community, only: [:show, :edit, :update, :destroy]
def index
@custom_communities = CustomCommunity.page(params[:page])
end
def show; end
def new
@custom_community = CustomCommunity.new
end
def edit; end
def create
@custom_community = CustomCommunity.new(custom_community_params)
respond_to do |format|
if @custom_community.save
format.html { redirect_to @custom_community, notice: "Custom community was successfully created." }
format.json { render :show, status: :created, location: @custom_community }
else
format.html { render :new }
format.json { render json: @custom_community.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if @custom_community.update(custom_community_params)
format.html { redirect_to @custom_community, notice: "Custom community was successfully updated." }
format.json { render :show, status: :ok, location: @custom_community }
else
format.html { render :edit }
format.json { render json: @custom_community.errors, status: :unprocessable_entity }
end
end
end
def destroy
@custom_community.destroy
respond_to do |format|
format.html { redirect_to admin_custom_communities_url, notice: "Custom community was successfully destroyed." }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_custom_community
@custom_community = CustomCommunity.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def custom_community_params
params.require(:custom_community).permit(:name, :description, :picture, :should_delete_picture)
end
end
型号:(文件:models/custom_community.rb)
# frozen_string_literal: true
class CustomCommunity < ApplicationRecord
end
迁移:
class CreateCustomCommunities < ActiveRecord::Migration[5.2]
def change
create_table :custom_communities do |t|
t.string :name
t.text :description
t.timestamps
end
end
end
路线: 在管理路线中:
resources :custom_communities
【问题讨论】:
-
我更改了模型的名称,现在,我尝试避免命名约定的复数/单数模式。运动部件越少越好。