Rails 3.1:在客户端应用程序中公开引擎帮助器的更好方法 [英] Rails 3.1: Better way to expose an engine's helper within the client app

查看:81
本文介绍了Rails 3.1:在客户端应用程序中公开引擎帮助器的更好方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现有几篇文章解决了消费(父)应用程序无法访问引擎中的帮助程序的问题.为了确保我们都在同一页面上,假设我们拥有以下内容:

I have found a few articles addressing the issue of helpers within an engine not being accessible to the consuming (parent) application. To make sure we are all on the same page, let's say we have this:

module MyEngine
  module ImportantHelper
    def some_important_helper
      ...do something important...
    end
  end
end

如果您查看 rails引擎隔离的引擎帮助器"(L293)中的文档,内容为:

If you look at the rails engine documentation in the "Isolated engine's helpers" (L293), it says:

  # Sometimes you may want to isolate engine, but use helpers that are defined for it.
  # If you want to share just a few specific helpers you can add them to application's
  # helpers in ApplicationController:
  #
  # class ApplicationController < ActionController::Base
  #   helper MyEngine::SharedEngineHelper
  # end
  #
  # If you want to include all of the engine's helpers, you can use #helpers method on an engine's
  # instance:
  #
  # class ApplicationController < ActionController::Base
  #   helper MyEngine::Engine.helpers
  # end

因此,如果我要求使用我的引擎的任何人将其添加到他们的application_controller.rb中,那么他们将可以访问我所有重要的帮助方法:

So if I ask anybody consuming my engine to add this to their application_controller.rb, then they will get access to all my important helper methods:

class ApplicationController < ActionController::Base
  helper MyEngine::ImportantHelper
end

这就是我想要的并且可以正常工作,但是这很痛苦,尤其是,如果按照我的用例,引擎公开的所有内容都可以/应该在使用该应用程序的任何地方使用.因此,我进一步研究了一下,找到了建议执行以下操作的解决方案:

This is what I want and it works, but that's kind of a pain, especially if, as is my use case, everything the engine exposes can/should be used anywhere in the consuming app. So I dug around a bit more and found a solution that suggested I do the following:

module MyEngine
  class Engine < Rails::Engine
    isolate_namespace MyEngine

    config.to_prepare do
      ApplicationController.helper(ImportantHelper)
    end
  end
end

现在这正是我想要的:将我所有的ImportantHelper方法添加到父应用程序的应用程序助手中.但是,它不起作用.有人可以帮我弄清楚为什么这种更好的解决方案不起作用吗?

Now this is exactly what I want: to add all my ImportantHelper method to the parent app's application helper. However, it doesn't work. Can anybody help me figure out why this more-better solution does not work?

我正在使用Rails 3.1.3运行ruby 1.8.7.让我知道是否错过了与此问题相关的任何重要信息,并在此先感谢.

I am running ruby 1.8.7 with rails 3.1.3. Let me know if I missed any important info germane to the issue, and thanks in advance.

推荐答案

您可以创建一个初始化器来完成此任务,如下所示:

You can create an initializer to accomplish this like so:

module MyEngine
  class Engine < Rails::Engine
    initializer 'my_engine.action_controller' do |app|
      ActiveSupport.on_load :action_controller do
        helper MyEngine::ImportantHelper
      end
    end
  end
end

这篇关于Rails 3.1:在客户端应用程序中公开引擎帮助器的更好方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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