3

我正在尝试从 Rails 应用程序中提取博客模型和控制器。我有一个名为 Rails 引擎Blog,我将把它安装/blog在主应用程序的路径上。

在我的Blog引擎中,我有一个PostsController正常的 CRUD 操作。问题是我想使用主要 rails 应用程序中的身份验证方法。

# app/controllers/blog/posts_controller.rb
module Blog
  class PostsController < ApplicationController
    # Basically I want to have access to the require_login method
    # from the main app.
    before_filter :require_login, only: [:new, :create]

    def new
      @post = Post.new
      authorize! :create, Post
    end
  end
end

而且我需要访问 User 模型,以便我可以检查 CanCan 的授权能力。例如,只允许管理员创建博客文章。

# app/models/blog/ability.rb
module Blog
  class Ability
    include CanCan::Ability

    def initialize(user)
      user ||= User.new

      # The user.admin? method is defined on the User class
      # from the main rails app.
      if user.admin?
        can [:create, :update], Post
      end
    end
  end
end

有没有办法完成这些事情?

4

1 回答 1

0

复制评论中的答案,以便从“未回答”过滤器中删除此问题:

看看我是如何解决类似问题的 - How get Devise's current_user method in Rails Engine

〜回答每个wildDAlex

或者这个 2,如何从 rails 3.1 引擎调用父应用程序的辅助方法

〜回答每个westonplatter

于 2013-10-09T07:01:41.337 回答