为什么我的Rails可挂载引擎不能正确加载帮助程序方法? [英] Why is my Rails mountable engine not loading helper methods correctly?

查看:84
本文介绍了为什么我的Rails可挂载引擎不能正确加载帮助程序方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



引擎的作用范围是它自己的命名空间。



在引擎中,有一个 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屋!

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