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

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

问题描述

我有一个使用 Devise gem 的 Rails 应用程序,我正在创建一个 Rails 引擎以安装在这个应用程序中.

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'

在引擎中,我想从主应用程序中获取 current_user 实例.

Within the Engine, I want to get the current_user instance from main application.

{main_app}/initializers/comments.rb

Comments.user_class = "User"
Comments.current_user = "current_user" #current_user is Devise method(works fine in app)

{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

当我调用 Comments.current_user 时,我收到错误错误的常量名称 current_user".

When I call Comments.current_user, I get the error "wrong constant name current_user".

我做错了什么?

推荐答案

Seams 我解决了问题.我会在 Comments 模型中调用 current_user 但它不起作用.但是从控制器获取 current_user 工作正常.但这不是优雅的方式(.

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 引擎中获取 Devise 的 current_user 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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