问题
I'm adding a mountable engine to my rails app, which provides a forum-like functionality (i.e. adds questions, answers, comments, etc). Everything works fine in development. In staging/production, however, I get an error when trying to create an answer, specifically a CanCan permission error (though I think that may be a red herring). But it feels like ti might be related to namespacing or name collision?
At any rate, the issue goes away when I set config.cache_classes = false
in my environment configuration file. But that's not an acceptable solution since it really slows things down.
Where do I go from here? How can I figure out which classes needs to be reloaded? What's the conventional way to deal with this problem?
I'm using Rails 3.1.
回答1:
I figured out the problem. It was an issue with boot order. My Engine::AnswersController
was being loaded before my Engine::ApplicationController
. And in my code, I was inheriting from just ApplicationController
. That means, since Engine::AnswerController
didn't know about Engine::ApplicationController
it was erroneously inheriting from the main app's ApplicationController
rather than the engine's ApplicationController
.
The fix was to force the answers controller to inherit from the engine rather than the main app by making the engine namespace explicit:
Engine::AnswersController < Engine::ApplicationController
I understand this problem is rather idiosyncratic to my application but how I arrived at the solution might be helpful to others. If you're faced with this type of problem, drop down into some type of debugger (I used Pry) and start inspecting things. Specifically, check what classes are loaded and look hard at the inheritance chain (i.e. Engine::ApplicationController.ancestors
). Look for anything that seems odd.
Also if the issue is engine related, you might need to place debuggers in both the engine and the main app. Debugging engines can be difficult since errors occurring in the engine might not necessarily bubble up to the main app.
Much thanks to tjbladez for helping me figure this out.
来源:https://stackoverflow.com/questions/7905618/setting-cache-classes-to-false-fixes-my-bug-what-to-do-next