我在书写审阅应用程序的书中创建了一个多态关系。我的应用程序有嵌套模型:Piece >> Section >> Subsection >> Subsubsection,并且我创建了一个属于所有这些称为KeyConcept的模型,我的意图是它们中的每一个都可以有一个关键概念。当试图展示“海贼王”模型我得到以下错误的“显示”视图:未初始化的常量模型::具有多态关系的错误
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文件是这样的:
在模型/忧虑/ conceptable.rbmodule 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([email protected]).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([email protected]).section_id
@piece = Section.find([email protected]).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是否可以工作,我得到零,因为我还没有创建任何实例。
我注意到,在错误消息中它说Keyconcept而不是camelcase KeyConcept。我认为这是问题所在,但我没有足够的经验来理解它。
我很感谢帮助解决这个问题!
你在初始描述中说模型是'KeyConcept',但是在你的关系等中,它指的是keyconcepts而不是key_concepts(它应该是如果C的概念是大写的),在我看来就像一个简单的拼写/大写问题? –