0

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

4

2 回答 2

4

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

module Blog
  class BlogsController < ApplicationController

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

module Blog
  class ApplicationController < ActionController::Base

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

module Blog
  class ApplicationController < ::ApplicationController

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

于 2011-09-19T18:43:48.863 回答
0

你应该调查一下

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

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

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

于 2011-12-08T05:12:52.000 回答