LoadError in Controller#index

ruby-on-rails controller polymorphic-associations autoloader

598 просмотра

1 ответ

I created a polymorphic relation in a book reviewing app that I am writing. My app has the nested models: Piece >> Section >> Subsection >> Subsubsection, and I have created a model which belongs to all of these called KeyConcept, my intention being that each of these can have a key concept.

I am getting an error that I don't understand when trying to display the index action of the keyconcepts controller. I think it might be due to a naming conflict but I don't have enough experience to understand it.

the error I am getting looks like this:

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)

My keyconcepts controller looks like this:

key_concepts_controller.rb class Key_conceptsController < ApplicationController include 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

the link comes from the show action of the parent Piece model here:

<% @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>

How do I link to the keyconcepts "show" action by the way? I havent been able to so i just linked to the index action :/

So the routes.rb file looks like this:

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 module Conceptable extend ActiveSupport::Concern

  included do
    has_many :keyconcepts, as: :conceptable
  end
end
Автор: Kingdavidek Источник Размещён: 08.11.2019 11:19

Ответы (1)


1 плюс

Well the problem is naming convention

Your key_concepts_controller class name should be

KeyConceptsController < ApplicationController

Also make sure you follow proper conventions

If your model name is KeyConcept file name must be key_concept.rb

Controller name should be KeyConceptsController and file name must be key_concepts_controller.rb

Same goes with routes

resources :  key_concepts

Refer this for more details

Автор: Deepak Mahakale Размещён: 20.08.2016 02:55
Вопросы из категории :
32x32