7

I was able recently to organize my code by grouping everything into folders.
I had an issue with having the same "group name" for both my group of controllers under the app/ directory and my module under the lib/ directory but I was able to fix by following this:

Rails: Same name for a library module and a group of controllers?

I also know that whenever you change your lib code, you need to restart the rails server which is totally fine by me.

But after the recent re-organization, every time I change the code in the controllers, I get the following error!!!

NameError at /admin
uninitialized constant Admin::PagerDuty

and to resolve it, I simply restart the server!!

Any advice?!

EDIT: STRUCTURE:

Controller main_controller.rb is under app/controllers/admin

class Admin::MainController < ApplicationController
end

Helper main_helper.rb is under app/helpers/admin

module Admin::MainHelper
      require "admin/pager_duty.rb"

      def pager_duty
        pagerduty = Admin::PagerDuty.new()
        @on_call = pagerduty.on_call()
        @counts = pagerduty.open_incidents()
      end
end

lib pager_duty.rb is under lib/admin

module Admin 
  class PagerDuty
   ....
  end
end
Community
  • 1
  • 1
  • So are you using autoloading for lib classes? Or explicit requires without autoloading? – jurglic Aug 17 '13 at 17:42
  • explicit, I'll edit the question to add more info. –  Aug 17 '13 at 17:43
  • You should not have to restart your server in development mode with the Rails default settings. To autoload your ruby file under the lib directory, add the following to your `config/application.rb` config file: `config.autoload_paths += %W(#{config.root}/lib)` – Pierre-Louis Gottfrois Aug 17 '13 at 18:00
  • The problem is not with restarting after changing lib code, it's with changing any code any where. –  Aug 17 '13 at 18:17

1 Answers1

12

Try changing

require "admin/pager_duty.rb"

to

require_dependency "admin/pager_duty.rb"

in your module.

zrl3dx
  • 7,699
  • 3
  • 25
  • 35
  • It's Rails internal method, regular `require` doesn't reload changes hence `uninitialized constant` error. – zrl3dx Aug 17 '13 at 18:43
  • I only faced this after re-organizing my files! thanks anyways –  Aug 17 '13 at 18:50