Controller#index中的LoadError

时间:2016-08-20 12:35:44

标签: ruby-on-rails controller polymorphic-associations autoloader

我在书写评论应用程序中创建了一个多态关系。我的应用程序有嵌套模型:Piece>>部分>>分段>>子集,我创建了一个属于所有这些名为KeyConcept的模型,我的意图是每个都可以有一个关键概念。

我在尝试显示keyconcepts控制器的索引操作时遇到了一个我不明白的错误。 我认为这可能是由于命名冲突,但我没有足够的经验来理解它。

我得到的错误如下:

Unable to autoload constant KeyConceptsController, expected /home/david/Documents/Learning/StuddyBuddy/app/controllers/key_concepts_controller.rb to define it

    else
      require_or_load(expanded, qualified_name)
      raise LoadError, "Unable to autoload constant #{qualified_name}, expected #{file_path} to define it" unless from_mod.const_defined?(const_name, false)
      return from_mod.const_get(const_name)
    end
  elsif mod = autoload_module!(from_mod, const_name, qualified_name, path_suffix)

我的keyconcepts控制器如下所示:

key_concepts_controller.rb     class Key_conceptsController< ApplicationController的     包括KeyconceptsHelper

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

        here = @piece
        parameter = :piece_id
        type = "Piece"
    elsif params[:section_id]
        @section = Section.find(params[:section_id])
        @piece = @section.piece_id
        @keyconcept = @section.key_concepts.find(params[: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 = redirect_to piece_path(@piece)
    elsif type == "Section"
        @redirect = redirect_to piece_section_path(@piece, @section)
    elsif type == "Subsection"
        @redirect = redirect_to piece_section_subsection_path(@piece, @section, @subsection)
    elsif type == "Subsubsection"
        @redirect = redirect_to piece_section_subsection_subsubsection_path(@piece, @section, @subsection, @subsubsection)
    end
end

def index
    whereami.call
    @piece = Piece.find(params[:piece_id])
    @keyconcept = @piece.key_concepts.find(params[:id])
    @redirect = redirect.call
end 

def show
    whereami.call
    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

链接来自父作品模型的show动作:

<% @piece.key_concepts.each do |concept| %>
<li>
  <%= link_to concept.definition, [@piece, @keyconcept]  %>
  <!-- we didn't use #piece_key_concept_path(@piece, @keyconcept), class: 'section_name' and it worked -->
</li>
  

如何链接到keyconcepts&#34; show&#34;动作顺便说一下?我还没有能够这样做,我只是链接到索引操作:/

所以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

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

in models / concerns / conceptable.rb     模块可以概念化       扩展ActiveSupport :: Concern

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

1 个答案:

答案 0 :(得分:1)

问题在于命名约定

您的key_concepts_controller类名应为

KeyConceptsController < ApplicationController

还要确保遵循适当的约定

如果您的型号名称为KeyConcept,则文件名必须为key_concept.rb

控制器名称应为KeyConceptsController,文件名必须为key_concepts_controller.rb

与路线相同

resources :  key_concepts

有关详细信息,请参阅this