How to prevent editing of certain field in a model?

48 views
Skip to first unread message

Nick

unread,
Sep 28, 2020, 7:19:25 PM9/28/20
to rails_admin
I have a model that I want certain admins to only be able to edit a single field on. I came across this PR in Cancancan and it looks promising, but I can't seem to get it to work with Rail Admin: https://github.com/CanCanCommunity/cancancan/pull/474

Things I've tried:
can :manage, MyModel, :myfield, user_id: user.id

can :manage, MyModel, user_id: user.id
cannot :manage, MyModel, :myfield

can :manage, MyModel, user_id: user.id
cannot :update, MyModel, :myfield

And several other combinations. Is it possible to prevent users from editing certain fields? Or is this not implemented for Rails Admin? If I wanted to do this, what is the best approach? I'm pretty new to Rails and Rails Admin so I don't have a great idea of what I'm doing.

Thanks

Message has been deleted

Roc Khalil

unread,
Sep 29, 2020, 5:35:40 AM9/29/20
to rails_admin
I thnk you can do something like this:

```
field :test do
   visible do
      bindings[:view]._current_user.admin?
   end
end
```

Nick

unread,
Sep 29, 2020, 1:50:55 PM9/29/20
to rails_admin
Thanks Roc. I came up with a similar solution last night after a little digging and it works:

field :test do
  visible do
    bindings[:controller].current_user.admin?
  end
end
Reply all
Reply to author
Forward
0 new messages