未初始化的常量模型::多态关系错误

Uninitialized constant Model:: Error with polymorphic relation

我在我正在编写的书评应用程序中创建了一个多态关系。我的应用程序有嵌套模型:Piece >> Section >> Subsection >> Subsubsection,我创建了一个属于所有这些的模型,称为 KeyConcept,我的意图是每个模型都可以有一个关键概念。尝试显示 "Piece" 模型的 "Show" 视图时出现以下错误:

NameError in Pieces#show
uninitialized constant Piece::Keyconcept

<h5>Summary: </h5><p><%= simple_format(@piece.summary) %></p>
<br/>
<% @piece.keyconcepts.each do |concept| %>
    <li>
      <%= link_to concept.definition, r, class: 'section_name' %>
    </li>

因此 routes.rb 文件如下所示:

resources :pieces do
  resources :sections do
    resources :subsections do 
      resources :subsubsections
    end
  end
  resources :links
end

resources :pieces, :sections, :subsections, :subsubsections do
  resources :connections, only: [:index, :new, :edit, :update, :destroy, :create]
  resources :keyconcepts, only: [:index, :new, :edit, :update, :destroy, :create, :show]
end

model.rb 文件如下所示:

在models/concerns/conceptable.rb

module Conceptable
  extend ActiveSupport::Concern

  included do
    has_many :keyconcepts, as: :conceptable
  end
end

key_concept.rb

class KeyConcept < ActiveRecord::Base
    belongs_to :conceptable, polymorphic: true
end

piece.rb

class Piece < ActiveRecord::Base
    include Connectable
    include Conceptable
    has_many :sections
    has_many :subsections, through: :sections
    has_many :links
end

不知道是不是控制器的问题?

class KeyconceptsController < ApplicationController
    include KeyconceptsHelper

    def whereami
        if params[:piece_id]
            @piece = Piece.find(params[:piece_id])

            here = @piece
            parameter = :piece_id
            type = "Piece"
        elsif params[:section_id]
            @section = ::Section.find(params[:section_id])
            @piece = @section.piece_id

            here = @section
            parameter = :section_id
            type = "Section"
        elsif params[:subsection_id]
            @subsection = Subsection.find(params[:subsection_id])
            @section = @subsection.section_id
            @piece = Section.find(id=@section).piece_id

            here = @subsection
            parameter = :subsection_id
            type = "Subsection"
        elsif params[:subsubsection_id]
            @subsubsection = Subsubsection.find(params[:subsubsection_id])
            @subsection = @subsubsection.subsection_id
            @section = Subsection.find(id=@subsection).section_id
            @piece = Section.find(id=@section).piece_id

            here = @subsubsection 
            parameter = :subsubsection_id
            type = "Subsubsection"
        end
end

def redirect
    if type == "Piece"
        redirect_to piece_path(@piece)
    elsif type == "Section"
        redirect_to piece_section_path(@piece, @section)
    elsif type == "Subsection"
        redirect_to piece_section_subsection_path(@piece, @section, @subsection)
    elsif type == "Subsubsection"
        redirect_to piece_section_subsection_subsubsection_path(@piece, @section, @subsection, @subsubsection)
    end
end

def index
    whereami.call
end 

def show
    whereami.call
    r = redirect.call
end

def new
    @keyconcept = KeyConcept.new
    @keyconcept.conceptable_id = here.id
end

def create
    whereami.call

  @keyconcept = KeyConcept.new(keyconcept_params)

  @keyconcept.conceptable_id = params[parameter]
  @keyconcept.conceptable_type = type
  @keyconcept.save

  redirect.call
end

def destroy
    here.destroy
    redirect.call
    flash.notice = "#{type} '#{here.name}'  from '#{@piece.name}' deleted!"
end

def edit
    whereami.call
end

def update
    whereami.call

    here.update(keyconcept_params)
    flash.notice = "#{type} '#{here.name}' Updated!"
    redirect.call
end
end

我重新加载了控制台,但我得到了同样的错误。我也尝试在控制台中做一些事情,这个:Piece.first.keyconcepts,不起作用(我得到相同的 NameError:未初始化的常量 Piece::Keyconcept)但是这个:KeyConcept.first 确实有效,我即使得到 nil 因为我还没有创建任何实例。

我注意到,在错误消息中它说的是 Keyconcept 而不是驼峰式 KeyConcept。我认为这是问题所在,但我没有足够的经验来理解它。

如果能帮助解决这个问题,我将不胜感激!

这里的问题是你没有遵循正确的约定

将您的型号名称更改为 Keyconcept,将 file_name 更改为 keyconcept.rb

keyconcept.rb

class Keyconcept < ActiveRecord::Base
    belongs_to :conceptable, polymorphic: true
end

您需要在以下位置将 keyconcepts 更改为 key_concepts

  • 路线

    resources :keyconcepts
    
  • 控制器名称

    class KeyConceptsController < ApplicationController
      ...
    end
    
  • 控制器文件名

    key_concepts_controller.rb
    
  • 强参数

    def key_concept_params
      params.require(:key_concept).permit(:your, :params)
    end
    
  • 协会

    has_many :key_concepts