如何在Rails Engine中获得Devise的current_user方法 [英] How get Devise's current_user method in Rails Engine
问题描述
mount Comments :: Engine => '/ talk',:as => 'comment'
在引擎内,我想获得 current_user
实例从主应用程序。
在 {main_app} /initializers/comments.rb
Comments.user_class =User
Comments.current_user =current_user#current_user是Devise方法(在应用程序中正常工作)
在 {引擎} /lib/comments.rb
code> requirecomments / engine
模块注释
mattr_accessor:user_class,:current_user
def self.user_class
@@ user_class .constantize
end
def self.current_user
send(@@ current_user)
end
end
当我调用 Comments.current_user
时,我收到错误常量名称current_user。
我做错了什么?
接缝我解决了问题。
我在评论模型中调用current_user,但它没有起作用。
从控制器获取 current_user
可以正常工作。
但这不是优雅的方式(。
I have a Rails application that's using the Devise gem, and I'm creating a Rails Engine to mount in this app.
mount Comments::Engine => '/talk', :as => 'comments'
Within the Engine, I want to get the current_user
instance from main application.
In {main_app}/initializers/comments.rb
Comments.user_class = "User"
Comments.current_user = "current_user" #current_user is Devise method(works fine in app)
In {engine}/lib/comments.rb
require "comments/engine"
module Comments
mattr_accessor :user_class, :current_user
def self.user_class
@@user_class.constantize
end
def self.current_user
send(@@current_user)
end
end
When I call Comments.current_user
, I get the error "wrong constant name current_user".
What am I doing wrong?
Seams I'm solved problem.
I'd call current_user within Comments model and it didn't work.
However getting current_user
from controller, works fine.
But this is not elegant way (.
这篇关于如何在Rails Engine中获得Devise的current_user方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!