我一直在使用rails_admin v0.7.0 并成功完成了清除gem。我今天尝试将 Rails_admin 更新到 v1.0,但收到未定义的变量或方法错误current_user
。在 v0.7.0 中似乎RailsAdmin::MainController
继承自ApplicationController
,而在 v1.0 中它直接继承自ActionController::Base
,这可以解释current_user
现在未定义(我相信current_user
定义于ApplicationController
与间隙宝石)。然而,由于我没有找到其他人遇到这个问题,我想我一定错过了一些东西。
我不是在这个应用程序上设置许可的人,但我不认为我们正在用它做任何会影响这一点的非标准行为。Clearance::Controller
包含在ApplicationController
。没有特殊定义current_user
.
配置/初始化程序/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
你是对的,Rails Admin 继承自::ActionController::Base
默认情况下,这就是导致您的问题的原因。幸运的是,修复方法很简单。添加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
我创建了一个参考仓库在这里 https://github.com/philtr/clearance-and-rails-admin如果您需要的话可以进行比较。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)