uninitialized constant for HomePresenter

僤鯓⒐⒋嵵緔 提交于 2019-12-22 10:45:20

问题


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?


回答1:


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)



回答2:


You also may want to set your autoload path to include the app/presenters folder. See the documentation for delegate_presenter on how to do this.

(Also, check out that gem - it may get you where you are going!)



来源:https://stackoverflow.com/questions/7925685/uninitialized-constant-for-homepresenter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!