ruby-on-rails - 覆盖 Rails 引擎 View /布局时访问主应用助手

标签 ruby-on-rails ruby-on-rails-3.1 rails-engines rails-3.1

我创建了一个简单的 Rails 引擎来为应用程序提供一些通用功能(照片库)。我希望能够覆盖标准的 _header 部分,以便画廊的菜单与我的主应用程序的菜单相匹配。在我的标题 View 中,我调用了一个属于 application_helpers(主应用程序)的帮助程序,但我不断收到“未定义方法”错误。据我所知,当我覆盖引擎应用程序布局或其部分时,主应用程序 application_helpers 没有被包含(显然)。

所以我的问题是,如何覆盖主应用程序中的引擎 View ,并访问主应用程序辅助方法?我仍然需要访问引擎助手以及不要搞砸引擎功能。

我还需要覆盖 Controller 吗?似乎很多只是为了得到一些 helper 。

谢谢

rails 3.1.3

最佳答案

查看此博文:http://www.candland.net/2012/04/17/rails-routes-used-in-an-isolated-engine/
作者在应用程序助手中添加了一个 method_missing 定义,以便访问父应用程序的助手。

/config/initializers/blogit.rb

module Blogit
    module ApplicationHelper
      def method_missing method, *args, &block
        puts "LOOKING FOR ROUTES #{method}"
        if method.to_s.end_with?('_path') or method.to_s.end_with?('_url')
          if main_app.respond_to?(method)
            main_app.send(method, *args)
          else
            super
          end
        else
          super
        end
      end

      def respond_to?(method)
        if method.to_s.end_with?('_path') or method.to_s.end_with?('_url')
          if main_app.respond_to?(method)
            true
          else
            super
          end
        else
          super
        end
      end
    end
  end

关于ruby-on-rails - 覆盖 Rails 引擎 View /布局时访问主应用助手,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9232175/

相关文章:

javascript - 在 jQuery 中处理 JSON 和 HTML 模板

ruby-on-rails - 使用 Ruby on Rails link_to 链接到 Controller 操作

ruby-on-rails - rails 引擎 : Extend model with application class

ruby-on-rails - 辅助测试中未定义的局部变量或方法 `main_app'

ruby-on-rails - 重用 named_scope 来定义另一个 named_scope

javascript - 从 javascript onclick 发出 ajax 请求

ruby-on-rails-3.1 - Rails.3.1.1 : config. assets.digest = true 导致编译后的 css 被用作 Sprite 的文件名

ruby-on-rails - 由于格式错误,Rails 导入 CSV 失败

ruby - Rails 4 引擎是否自动加载引擎的 i18n 语言环境文件?

ruby-on-rails - View 渲染后 Rails 中的回调(用于记录目的)