无法访问 AcitveAdmin 表单定义中的设备 current_admin_user

发布于 2024-12-17 22:03:43 字数 526 浏览 2 评论 0原文

我需要仅向特定用户显示 ActiveAdmin 表单中的某些表单字段。 但是,当我尝试使用此代码检查用户状态时:

ActiveAdmin.register Store do
  # ...
  form do |f|
    f.inputs "Basic" do
      if current_admin_user.super_admin?
        f.input :admin_user
      end
     # ...
     end
  end
end

我发现

undefined local variable or method `current_admin_user' for #<ActiveAdmin::DSL:0xdb8e798>

CanCan 方法在 ActiveAdmin 表单定义中也不起作用。

一般来说,我的问题是:如何根据当前用户类型管理管理界面显示? 特别是,如何从 ActiveAdmin 定义中获取当前的设备用户对象?

I need to display some form fields in ActiveAdmin form only to specific users.
But when I try to check user status with this code:

ActiveAdmin.register Store do
  # ...
  form do |f|
    f.inputs "Basic" do
      if current_admin_user.super_admin?
        f.input :admin_user
      end
     # ...
     end
  end
end

I get

undefined local variable or method `current_admin_user' for #<ActiveAdmin::DSL:0xdb8e798>

CanCan methods also don't work in the ActiveAdmin form definition.

Generally my question is: how can I manage admin interface display, based on current user type?
Particularly, how can I get current devise user object from within ActiveAdmin definitions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

从来不烧饼 2024-12-24 22:03:43

这是一个范围问题。您可以尝试使用 f.template 对象访问辅助方法,如下所示:

ActiveAdmin.register Store do
  # ...
  form do |f|
    f.inputs "Basic" do
      if f.template.current_admin_user.super_admin?
        f.input :admin_user
      end
     # ...
     end
  end
end

祝您好运。

It is a matter of scope. You could try accessing the helper method using the f.template object like so:

ActiveAdmin.register Store do
  # ...
  form do |f|
    f.inputs "Basic" do
      if f.template.current_admin_user.super_admin?
        f.input :admin_user
      end
     # ...
     end
  end
end

Good luck.

初见 2024-12-24 22:03:43

我找到了解决此问题的方法。在 /app/admin/stores.rb: 中

ActiveAdmin.register Store do
  # ...
  form :partial => 'form'
  # ...
end

,然后在 /app/views/admin/stores/_form.html.haml: 中:

= semantic_form_for [:admin, @store] do |f|
  = f.inputs "Basic" do
    - if current_admin_user.super_admin?
      =f.input :admin_user

这一点都不方便,但可以。

I've found a workaround for this issue. In /app/admin/stores.rb:

ActiveAdmin.register Store do
  # ...
  form :partial => 'form'
  # ...
end

and then in /app/views/admin/stores/_form.html.haml:

= semantic_form_for [:admin, @store] do |f|
  = f.inputs "Basic" do
    - if current_admin_user.super_admin?
      =f.input :admin_user

It's not convenient at all, but works.

和影子一齐双人舞 2024-12-24 22:03:43

我知道这个线程有点旧,但我刚刚找到了一个很好的小解决方案。我没有使用 devise,而是使用 Twitter API 来登录我的用户

active_admin.rb 中,查找 config.current_user_method 并将默认值更改为 < code>current_user from current_admin_user

config.current_user_method = :current_user

同时将 logout_link_path 更改为您的路由包含 as: :destroy_user_session

I know this thread is a little old but I just found a nice little fix for this. I'm not using devise, I'm using the Twitter API for logging in my users

In active_admin.rb, look for config.current_user_method and change the default value to current_user from current_admin_user

config.current_user_method = :current_user

Also change the logout_link_path to in your routes to include as: :destroy_user_session

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文