Change the type of field in resource depending on ...
# avo-2
r
Change the type of field in resource depending on user role
l
Hey Angel
yes, that's possible
use the
visible
option to show/hide the fields based on the
current_user
Copy code
ruby
field :something, as: :select, options: ["some_options"], visible: ->(resource:) { current_user.is_admin? }
field :something, as: :select, options: ["other_options"], visible: ->(resource:) { !current_user.is_admin? }
r
Hi Adrian. Thanks for your answer. I can se the solution, but the issue is with
current_user
I have defined
current_user
in avo.rb initializer and I have the latest version 2.28, launched this week as I remember...
l
can you try
Avo::App.current_user
instead?
r
With Avo::App.current_user works fine, but I can not change the type of field.
field :client, as: :hidden, default: -> { ::Avo::App.context[:user].client.id }, visible: ->(resource:) { ::Avo::App.context[:user].client? } field :client, as: :belongs_to, visible: ->(resource:) { ::Avo::App.context[:user].admin? }
l
maybe I didn't understand what you are trying to do. can you please explain it again?
r
In the same resource, put the same field as different types depending on the user role: For admin: A select to select the client For client: A hidden field with the client_id value.
l
and this doesn't work?
it should
please output
::Avo::App.context[:user].client?
and
::Avo::App.context[:user].admin?
and see what they return
to cover our bases