如何在Rails Engine中获得Devise的current_user方法 [英] How get Devise's current_user method in Rails Engine

查看:197
本文介绍了如何在Rails Engine中获得Devise的current_user方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用Devise宝石的Rails应用程序,我正在创建一个Rails引擎来安装在这个应用程序中。

  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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆