Search code examples
ruby-on-railscontrollerpresenter

uninitialized constant for HomePresenter


I'm trying to implement a HomePresenter to be used inside the home action of my Pages controller:

# app/controllers/pages_controller.rb
class PagesController < ApplicationController
   def home
      @presenter = Pages::HomePresenter.new(current_user)
   end
   ...
end

# app/presenters/pages/home_presenter.rb
module Pages
   class HomePresenter
      def initialize(user)
         @user = user
      end
      ...
   end
end

My presenter specs pass without errors, but when I run the server and access the home page in Chrome, I get this:

uninitialized constant ActionController::Caching::Pages::HomePresenter

For two other models in my app, I'm using IndexPresenters that are almost identical to this one with regard to naming convention and directory structure, but neither of them gives this error.

Found a similar, yet unanswered post here:

Name conflict between controller name and presenter namespace

Any ideas?


Solution

  • Figured this out, answered it here:

    Name conflict between controller name and presenter namespace

    Basically, change

    @presenter = Pages::HomePresenter.new(current_user)
    

    to

    @presenter = ::Pages::HomePresenter.new(current_user)