I am new to ruby-on-rails and i am looking for a way to have my rails_admin dashboard accessible only by superadmin users. I discovered that the rails_admin gem is fully compatible with another gem: cancancan, used for managing authorizations.
I followed this guide: https://github.com/sferik/rails_admin/wiki/Cancancan to configure rails_admin properly, and this is the result:
config/initializers/rails_admin.rb :
RailsAdmin.config do |config|
# == Devise ==
config.authenticate_with do
warden.authenticate! scope: :user
end
config.current_user_method(&:current_user)
# == CancanCan ==
config.authorize_with :cancancan
config.actions do
dashboard # mandatory
index # mandatory
new
export
bulk_delete
show
edit
delete
show_in_app
end
end
app/models/ability.rb :
class Ability
include CanCan::Ability
def initialize(user)
user ||= User.new
can :read, :all
if user.superadmin_role?
can :access, :rails_admin
can :read, :dashboard
can :manage, :all
elsif user.admin_role?
can :manage, :all
end
end
end
When i created the project, i skipped active-records. For now, the only scaffold i made was for a class called Product.
I keep getting this error when navigating to localhost:3000/admin:
NoMethodError in RailsAdmin::MainController#dashboard
undefined method `accessible_by' for Product:Class
I cannot figure out where and how to define this "accessible_by" method. Moreover, i found that on this link https://github.com/CanCanCommunity/cancancan/wiki/Fetching-Records , is told that NOT using active records requires a model adapter.
I'm using on MacOS 11.15.6 and:
Can someone help me understanding what am i doing wrong? Thanks:)
Ok, i got it!
I solved the problem with these steps:
FIRST OF ALL: STOP rails server
IF CURRENTLY RUNNING
Install the cancancan-mongoid
gem. Simply type gem 'cancancan-mongoid'
in your Gemfile, UNDER the existing declaration of the cancancan gem. Then run bundle install
. I referred to this page: https://github.com/CanCanCommunity/cancancan-mongoid
Check that the order of the gems in your Gemfile looks like this:
mongoid
cancancan
cancancan-mongoid
rails_admin
The first three steps should solve the problem, but if it persists, proceed with this last step:
cancan_mongoid.rb
and place it in your config/initializers
folder, then paste the full code shown here: https://gist.github.com/bowsersenior/561639This worked for me: :)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.