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?
wildDAlex
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