【问题标题】:Rails - NameError - uninitialized constantRails - NameError - 未初始化的常量
【发布时间】: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

【问题讨论】:

  • 我更改了模型的名称,现在,我尝试避免命名约定的复数/单数模式。运动部件越少越好。

标签: ruby-on-rails nameerror


【解决方案1】:

我相信其他人可以解释原因,但有时 rails 会认为 index 方法中的以下类:

class Admin::CustomCommunitiesController < Admin::BaseController
  def index
    CustomCommunity.all
  end
end

正在引用Admin::CustomCommunitiesController 中定义的类,即它认为您正在尝试调用Admin::CustomCommunitiesController::CustomCommunity.all。要“取消命名空间”类,请尝试:

# 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

我想这就是全部。基本上将每个 CustomCommunity 类调用切换到 ::CustomCommunity 以取消命名空间。

【讨论】:

  • 这可能是一个临时解决方案,但无法弄清楚为什么 rails 在控制器类下进行命名空间。这是我第一次看到这样的东西。
  • 我在各种项目中间歇性地得到它,并且从来没有花足够的时间研究它 - 它并不漂亮,但据我所知,这个修复没有任何缺点
  • 问题是我不知道为什么会这样。这只会导致新的错误。
猜你喜欢
  • 1970-01-01
  • 2016-07-15
  • 2017-07-10
  • 2022-01-01
  • 2016-10-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多