Search code examples
ruby-on-railsrubygemsexternal

Rails Devise - How to authenticate user inside initializers


A rails app using external coupon gem and devise to block coupon pages from being used from non-admin user. in my initializer/coupons.rb trying to do something like this:

  config.authorizer = proc do |controller|
      if !current_user.admin?

        redirect_to(request.referrer || "/")
      end
  end

getting this one: undefined local variable or method `current_user' for main:Object

Having a look at the application controller's gem shows authorizer initializer call in before action filter.

Any thoughts?


Solution

  • I believe to reference user on models/initializers break the MVC architeture principles. The logic about the current_user should be in controllers and views. Consider refactoring your logic, please. Thanks!