将"cache_classes"设置为"false"可修复我的错误.接下来做什么? [英] Setting `cache_classes` to `false` fixes my bug. What to do next?

查看:94
本文介绍了将"cache_classes"设置为"false"可修复我的错误.接下来做什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在向我的Rails应用程序添加一个可安装的引擎,该引擎提供了类似于论坛的功能(即添加问题,答案,评论等).在开发过程中一切正常.但是,在登台/制作中,尝试创建答案时出现错误,特别是CanCan权限错误(尽管我认为这可能是一个红色的鲱鱼).但是感觉ti可能与命名空间或名称冲突有关?

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?

无论如何,当我在环境配置文件中设置config.cache_classes = false时,问题就消失了.但这不是一个可以接受的解决方案,因为它确实会使速度变慢.

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?

我正在使用Rails 3.1.

I'm using Rails 3.1.

推荐答案

我发现了问题所在.这是启动顺序的问题.我的Engine::AnswersController在我的Engine::ApplicationController之前被加载.在我的代码中,我只是从ApplicationController继承而来.这意味着,由于Engine::AnswerController不了解Engine::ApplicationController,因此它是错误地继承自主应用程序的ApplicationController而不是引擎的ApplicationController.

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.

解决方法是通过明确显示引擎名称空间来强制Answers控制器从引擎而不是主应用程序继承:

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

我知道这个问题对我的应用程序来说很特殊,但是我如何得出解决方案可能对其他人有帮助.如果您遇到此类问题,请进入某种类型的调试器(我使用了 Pry )并开始检查东西.具体来说,请检查加载了哪些类,并仔细查看继承链(即Engine::ApplicationController.ancestors).寻找任何看起来很奇怪的东西.

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.

非常感谢 tjbladez 帮助我解决了这个问题.

Much thanks to tjbladez for helping me figure this out.

这篇关于将"cache_classes"设置为"false"可修复我的错误.接下来做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆