0

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

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.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

片断.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。我认为这就是问题所在,但我没有足够的经验来理解它。

我将不胜感激帮助解决这个问题!

4

1 回答 1

0

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

将您的模型名称更改为Keyconcept并将文件名更改为keyconcept.rb

关键概念.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
    
于 2016-08-19T05:21:51.677 回答