我正在写一个Rails引擎。
我在应用程序控制器内部的gem中有两种方法(authenticate和current_user)。https://github.com/krunal/aadhar/blob/master/app/controllers/aadhar/application_controller.rb
我希望'authenticate'方法在父Rails应用程序中可以作为before_filter使用。
我希望'current_user'方法可以作为父Rails应用程序中的方法和助手使用。
我不知道该如何实现。
假设在我的父应用程序中,我有一个职位控制器。我想通过这种方式使用“身份验证”和“当前用户”。
PostsController < ApplicationController
before_filter :authenticate
def index
current_user.posts
end
参考-一种从引擎向应用程序添加before_filter的方法
1)在lib / aadhar /中创建了一个新文件authenticate.rb。authenticate.rb
2)lib / aadhar.rb中的必需authenticate.rb aadhar.rb
3)在lib / aadhar / engine.rb engine.rb中添加了以下几行
ActiveSupport.on_load(:action_controller) do
include Aadhar::Authenticate
end
4)Authenticate和current_user方法现在在我的父Rails应用程序中可用。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句