繁体   English   中英

Rails 3.1 Engine:如何让引擎application_controller与客户端应用程序的application_controller对话?

[英]Rails 3.1 Engine: How do I get the engine application_controller to talk to the client app's application_controller?

我有一个新的可安装的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对话吗?

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

module Blog
  class BlogsController < ApplicationController

然后,我查看了引擎的应用程序控制器,并看到了以下内容:

module Blog
  class ApplicationController < ActionController::Base

问题是我希望我的引擎先查看它的application_controller,然后再查看主应用程序的application_controller(如果什么也没找到),所以我改成这个,一切正常:

module Blog
  class ApplicationController < ::ApplicationController

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

你应该看看

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM