为什么我的Rails可挂载引擎不能正确加载帮助程序方法? [英] Why is my Rails mountable engine not loading helper methods correctly?
问题描述
引擎的作用范围是它自己的命名空间。
在引擎中,有一个 MyEngine :: ApplicationHelper
模块,它添加了一堆视图帮助器方法。
在我的应用程序布局中,我引用了其中的一些方法。
当我第一次以开发模式加载任何页面时,我得到一个 NoMethodError
,抱怨该方法(在gem的 ApplicationHelper
中定义)不存在。
一旦我在我的应用程序中编辑 ApplicationController
,问题就会自行解决。
有些东西告诉我这是直至最近Rails自动加载的变化;我使用的是Rails 3.2.2
我不能为我的生活找出为什么这不能正常工作:/ $ / b
我认为Rails指南有答案 here 。
要在您的应用中包含引擎中的特定助手:
class ApplicationController< ActionController :: Base
helper MyEngine :: ApplicationHelper
end
包含全部
class ApplicationController< ActionController :: Base
helper MyEngine :: Engine.helpers
end
I've built a rails gem that mounts as an engine.
The engine is scoped to it's own namespace.
In the engine, there's an MyEngine::ApplicationHelper
module which adds a bunch of view helper methods.
In my application layout, I refer to some of these methods.
When I first load any of the pages in development mode I get a NoMethodError
, complaining that the method (defined in the gem's ApplicationHelper
) doesn't exist.
Once I edit ApplicationController
within my app, the problem corrects itself.
Something tells me this is down to the recent changes in Rails's auto-loading; I'm using Rails 3.2.2
I can't for the life of me work out why this isn't working properly though :/
I think the Rails guides has the answer here.
To include that particular helper from your Engine in your app:
class ApplicationController < ActionController::Base
helper MyEngine::ApplicationHelper
end
To include all helpers from your Engine in your app:
class ApplicationController < ActionController::Base
helper MyEngine::Engine.helpers
end
这篇关于为什么我的Rails可挂载引擎不能正确加载帮助程序方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!