我已经安装了 rails_admin
gem,没有任何错误,它的显示模型也很糟糕,但是我有一个要求,我需要显示 current_user
登录的相关数据
e.g User has many Books so in rails admin i want only that user book but currently it's showing all users books which is default behaviour of rails_admin
我也尝试使用 cancancan
gem 来实现同样的事情,但它不起作用我的 rails_admin
初始化配置如下
rails_admin.rb
RailsAdmin.config do |config|
### Popular gems integration
## == Devise ==
config.authenticate_with do
warden.authenticate! scope: :user
end
config.current_user_method(&:current_user)
config.parent_controller = 'ApplicationController'
## == Cancan ==
config.authorize_with :cancan,UserAbility
## == Pundit ==
# config.authorize_with :pundit
config.included_models = ["Book","User"]
config.actions do
dashboard # mandatory
index # mandatory
new
export
bulk_delete
show
edit
delete
show_in_app
end
end
UserAbility类实现如下
class UserAbility
include CanCan::Ability
def initialize(user)
# Define abilities for the passed in user here. For example:
if user.present?
can :access, :dashboard
can :manage, :Book,id:user.id
end
end
end
最佳答案
代替 can :manage, :Book,id:user.id
,尝试使用:
can :read, Book, user_id: user.id
所以这些能力看起来像:
can :access, :rails_admin
can :dashboard
can :read, Book, user_id: user.id
关于ruby-on-rails - 在 rails_admin 中获取登录用户关联记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43030045/