我有一個Rails 3.2.x應用程序在後端使用了rails_admin
gem。我也在使用Devise進行身份驗證,並在User
模型上有一個角色字段來說明用戶是否是管理員或員工。Rails Admin config.authorize_with重定向用戶如果不是管理員
我試圖做的是建立了rails_admin授權,所以如果你訪問http://domain.com/admin只會讓它,如果current_user.role == "admin"
否則重定向到我家的控制器索引路徑home_index_path
我設置的是初始應該這樣做,並且如果角色不是管理員,它會拒絕管理員訪問,但是我收到以下錯誤:No route matches {:controller=>"home"}
。我想這可能與routes.rb
有關,而我的RailsAdmin線路安裝在routes.rb
中,但我不確定。最終,這應該檢查角色以確保它等於admin,否則,redirect_to home_index_path並刷新消息。
任何幫助,非常感謝。
rails_admin.rb
RailsAdmin.config do |config|
config.authorize_with do |controller|
unless current_user.role == 'admin'
flash[:error] = "You are not an admin"
redirect_to home_index_path
end
end
end