How get Devise's current_user method in Rails Engine

…衆ロ難τιáo~ 提交于 2019-12-02 00:55:25

问题


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?


回答1:


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 (.



来源:https://stackoverflow.com/questions/10668874/how-get-devises-current-user-method-in-rails-engine

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!