'current_user' undefined in rails_admin with permission

I am using rails_admin v0.7.0 with successfully achieving this permission. I tried updating rails_admin to v1.0 today, but getting an undefined variable or method error for current_user . In v0.7.0, it seems that RailsAdmin::MainController inherits from ApplicationController , while in v1.0 it inherits directly from ActionController::Base , which explains that current_user now undefined (I believe that current_user is defined in ApplicationController with a gap). However, since I do not find anyone else with this problem, I think I should miss something.

I was not the one who installed the permission for this application, but I don’t think that we are doing something non-standard with this, which will affect it. Clearance::Controller included in the ApplicationController . There is no specific definition for current_user .

configurations / Initializers / rails_admin.rb

 RailsAdmin.config do |config| # Popular gems integration ## Clearance config.authorize_with do |controller| unless current_user.admin? redirect_to( main_app.root_path, alert: "You are not permitted to view this page" ) end end config.current_user_method { current_user } end 
+5
source share
1 answer

You are right that the Rails Admin inherits from ::ActionController::Base by default, and that is what causes your problem. Fortunately, the fix is ​​simple. Add config.parent_controller = "::ApplicationController" to config/initializers/rails_admin.rb :

 RailsAdmin.config do |config| ## == Clearance == config.parent_controller = "::ApplicationController" config.authorize_with do |controller| unless current_user && current_user.admin? redirect_to( main_app.root_path, alert: "You are not permitted to view this page" ) end end # You actually don't need this line # config.current_user_method { current_user } end 

I created a reference repo here to compare if you need it.

+8
source

Source: https://habr.com/ru/post/1258005/


All Articles