【问题标题】:Rails 3.1 Engine: How do I get the engine application_controller to talk to the client app's application_controller?Rails 3.1 引擎:如何让引擎 application_controller 与客户端应用程序的 application_controller 对话?
【发布时间】:2011-09-19 06:33:32
【问题描述】:

我有一个新的、可安装的 rails 3.1 引擎,我需要客户端应用程序,即包含此引擎的 rails 应用程序,以定义一个基于权限的通用方法。

所以,我想要在我的引擎的博客控制器中说如下内容:

before_filter :redirect_unless_admin

然后我想将它留给客户端应用程序来定义谁是管理员。但是,每当我尝试这个时,我都会得到:

NameError in Blog::BlogsController#show

undefined local variable or method `redirect_unless_admin' for #<Blog::BlogsController:0x000001058aa038>

我的客户端应用控制器如下所示:

class ApplicationController < ActionController::Base

  # Required by blog engine
  def redirect_unless_admin
    if !logged_in?
      set_session_redirect
      redirect_to login_path, :notice => 'Please log in.'
    elsif !current_user.admin?
      set_session_redirect
      redirect_to root_path, :notice => 'You do not have permission to view this page.'
    end
  end

在我的引擎应用控制器中,我有以下内容:

module Blog
  class ApplicationController < ActionController::Base
  end
 end

谁能告诉我如何设置它,以便我的引擎的博客控制器可以与我的客户的 application_controller 通信?

【问题讨论】:

  • 我觉得this question的回答和你的情况有关。
  • 德米特里,那篇文章与我所需要的相反。该票询问“我如何使引擎中的方法可用于应用程序”。我需要知道的是“如何使应用程序中的方法对引擎可用?”。此外,这是一个 3.1 引擎和应用程序,所以我的假设是为我们处理任何类型的帮助程序、视图、资产等加载(这就是为什么这让我如此困惑)。

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


【解决方案1】:

答案最终变得痛苦而简单。在我的引擎中,我的博客控制器有以下内容:

module Blog
  class BlogsController < ApplicationController

然后我查看了我的引擎的应用程序控制器并看到了这个:

module Blog
  class ApplicationController < ActionController::Base

问题是我希望我的引擎查看它的 application_controller,然后如果没有找到主应用程序的 application_controller,所以我改成了这个,一切都很好:

module Blog
  class ApplicationController < ::ApplicationController

如果您知道不同/更好/更优雅/最佳实践/任何解决方案,我很想听听。

【讨论】:

    【解决方案2】:

    你应该看看

    rails plugin new forum --full        # Engine
    rails plugin new forum --mountable   # Mountable App
    

    引擎就像父应用程序的扩展。因此,您应该能够调用应用程序 helper_methods。

    可挂载应用与父应用隔离。我相信您正在使用可安装的应用程序。考虑使用上述命令更改为 rails 引擎。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-02-06
      • 2011-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-02
      相关资源
      最近更新 更多