我如何获得“current_user”在“可见的事情”中config/initializers/rails_admin.rb 中的块
我正在尝试实现与此类似的东西:
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
我知道在自述文件中它说该示例是理论上的,但我不断发现在该块中找不到 current_user 。我正在使用 CanCan 进行授权:
RailsAdmin.authorize_with :cancan
在 config/initializers 中的rails_admin.rb 顶部
谁能告诉我如何在可见块中获取 current_user 可用?如果用户是管理员,我只想在“列表”视图中显示某些字段。
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:
RailsAdmin.authorize_with :cancan
At the top of rails_admin.rb in config/initializers
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
由于 current_user 作为助手包含在视图中,因此您可以通过绑定哈希来获取它:
我从另一个问题中得到了这个想法: 如何在使用rails_admin创建新模型时将用户设置为current_user
有关的更多信息wiki 中的rails_admin 绑定: https://github.com/sferik/rails_admin/wiki/ Railsadmin-DSL
希望有帮助。
Since current_user is included as a helper in the view, you can get it through the bindings hash:
I got the idea from this other question: How to set the user to current_user when creating a new model using rails_admin
More info about rails_admin bindings in its wiki: https://github.com/sferik/rails_admin/wiki/Railsadmin-DSL
Hope it helps.
初始化程序在应用程序启动时运行一次,因此我认为您无法从这样的初始化程序中获取当前用户。
Initializers are run once on application startup, so I don't think you can get a current user from within an initializer like this.
我自己没有尝试过,但您可能会发现这很有用:
https://github.com/sferik /rails_admin/issues/559
简而言之,在rails_admin初始值设定项中定义
RailsAdmin::Config.current_user_method
。I have not tried this myself, but you may find this useful:
https://github.com/sferik/rails_admin/issues/559
In short, define
RailsAdmin::Config.current_user_method
in the rails_admin initializer.