I'm trying to implement something similar to this:
我正在尝试实现类似的东西:
RailsAdmin.config do |config|
config.model Team do
list do
field :name
field :created_at
field :revenue do
visible do
current_user.roles.include?(:accounting) # metacode
end
end
end
end
end
I know in the README it says the example is theoretical, but I keep getting that current_user is not found within that block. I'm authorizing with CanCan:
在README中,我知道这个例子是理论上的,但是我一直得到,current_user没有在那个块中找到。我和康康舞授权:
RailsAdmin.authorize_with :cancan
RailsAdmin。authorize_with:康康舞
At the top of rails_admin.rb in config/initializers
在rails_admin顶部。rb配置/初始值设定项
Can anyone tell me how to get current_user available within the visible block? I'd like to only show certain fields in the "List" view if a user is an admin.
谁能告诉我如何使current_user在可见块中可用吗?如果用户是管理员,我只希望在“列表”视图中显示某些字段。
3 个解决方案
#1
2
Since current_user is included as a helper in the view, you can get it through the bindings hash:
因为current_user作为助手包含在视图中,所以可以通过绑定散列:
visible do
bindings[:view].current_user.has_role? :admin # This works for Devise
end
I got the idea from this other question: How to set the user to current_user when creating a new model using rails_admin
我从另一个问题中得到了这个想法:如何在使用rails_admin创建新模型时将用户设置为current_user
More info about rails_admin bindings in its wiki: https://github.com/sferik/rails_admin/wiki/Railsadmin-DSL
更多关于rails_admin绑定的信息,请访问它的wiki: https://github.com/sferik/rails_admin/wiki/Railsadmin-DSL
Hope it helps.
希望它可以帮助。
#2
1
Initializers are run once on application startup, so I don't think you can get a current user from within an initializer like this.
初始化器在应用程序启动时只运行一次,所以我认为您无法从这样的初始化器中获得当前用户。
#3
0
I have not tried this myself, but you may find this useful:
我自己没有尝试过,但你会发现这很有用:
https://github.com/sferik/rails_admin/issues/559
https://github.com/sferik/rails_admin/issues/559
In short, define RailsAdmin::Config.current_user_method
in the rails_admin initializer.
简而言之,定义RailsAdmin::配置。rails_admin初始化器中的current_user_method。
#1
2
Since current_user is included as a helper in the view, you can get it through the bindings hash:
因为current_user作为助手包含在视图中,所以可以通过绑定散列:
visible do
bindings[:view].current_user.has_role? :admin # This works for Devise
end
I got the idea from this other question: How to set the user to current_user when creating a new model using rails_admin
我从另一个问题中得到了这个想法:如何在使用rails_admin创建新模型时将用户设置为current_user
More info about rails_admin bindings in its wiki: https://github.com/sferik/rails_admin/wiki/Railsadmin-DSL
更多关于rails_admin绑定的信息,请访问它的wiki: https://github.com/sferik/rails_admin/wiki/Railsadmin-DSL
Hope it helps.
希望它可以帮助。
#2
1
Initializers are run once on application startup, so I don't think you can get a current user from within an initializer like this.
初始化器在应用程序启动时只运行一次,所以我认为您无法从这样的初始化器中获得当前用户。
#3
0
I have not tried this myself, but you may find this useful:
我自己没有尝试过,但你会发现这很有用:
https://github.com/sferik/rails_admin/issues/559
https://github.com/sferik/rails_admin/issues/559
In short, define RailsAdmin::Config.current_user_method
in the rails_admin initializer.
简而言之,定义RailsAdmin::配置。rails_admin初始化器中的current_user_method。