在我的rails应用程序中,我正在使用devise
身份验证,并且我的模型名称user
具有一个Boolean字段admin
。在我的文章中routes.rb
,我试图限制除管理员以外的所有用户访问URL。
mount Resque::Server.new, :at => "/resque"
。我尝试使用devise的current_user帮助器,例如
mount Resque::Server.new, :at => "/resque" if current_user.admin == true
但是得到了错误undefined local variable or method 'current_user' for #<ActionDispatch::Routing::Mapper:0x000000053ac780> (NameError)
。这该怎么做?我是新手,请帮忙
终于这对我有用
# routes.rb
match "/resque" => Resque::Server, :anchor => false, :constraints => lambda { |req|
req.env['warden'].authenticated? and req.env['warden'].user.is_admin?
}
# user.rb
class MUserLogin < ActiveRecord::Base
.
.
.
def is_admin?
# your admin logic here for example:
self.admin == true
end
end
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句