我在书写评论应用程序中创建了一个多态关系。我的应用程序有嵌套模型:Piece>>部分>>分段>>子分段,我已经创建了一个属于所有这些名为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文件如下所示:
in 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 DOES工作,我即使我没有,因为我还没有创建任何实例。
我注意到,在错误消息中它表示Keyconcept而不是camelcase KeyConcept。我认为这就是问题所在,但我没有足够的经验来理解它。
我很感激帮助解决这个问题!
答案 0 :(得分:0)
这里的问题是你没有遵循正确的约定
将您的模型名称更改为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