I m trying to follow the directions in
# avo-2
a
I'm trying to follow the directions in the docs to limit Resource Fields to admins. Getting a context error. 🧵
from here, I'm following the 'dont use if statements, use with_options' bit. https://docs.avohq.io/2.0/field-options.html

https://cdn.discordapp.com/attachments/1129864672351367248/1129864895710634024/image.pngâ–¾

https://cdn.discordapp.com/attachments/1129864672351367248/1129864956406403132/image.pngâ–¾

goal here is to hide :account for non-admin users
l
Hi there, try
context[:user].admin?
l
hey @acceptable-cartoon-88076 please share your
avo.rb
config.set_context
setting
a
ok, I assumed there was some defaults in context, so it was empty. For now I stole the block from the Avo test config:
Copy code
config.set_context do
    {
      foo: "bar",
      user: current_user,
      params: request.params
    }
  end
However, now I have a new problem. This syntax, copied from the docs, and applied to an id field:
field :id, as: :id, visible: -> (resource:) { context[:user].is_admin? }
generates an Argument error wrong number of arguments (given 1, expected 0) If I replace the context[] call with a boolean, it works fine, but calling context causes the error.

https://cdn.discordapp.com/attachments/1129864672351367248/1130161315181244466/image.pngâ–¾

TIL I can use backticks to format code blocks, hooray!
l
Copy code
ruby
field :id, as: :id, visible: -> (resource:) { 
  puts ['context->', context].inspect;
  true
}
try this and please send me the output of the
puts
statement in the rails console
a
same arg error

https://cdn.discordapp.com/attachments/1129864672351367248/1130497120944791673/image.pngâ–¾

l
yes, but what do you ge tin the console? in the terminal?
you should get some output from that
puts
l
Hi guys, i think there is a conflict between only_on and visible block
@acceptable-cartoon-88076 try this code outside of
with_options only_on: :index
....
field :id, as: :id, visible: -> (resource:) { resource.view == :index && context[:user].is_admin?}
a
ok
(and there's nothing in the console since the puts statement raises the exception when evaluating what to output)
good catch, Paul, that worked as expected
l
Use it for now until we fix it
a
sounds good
l
Update: I discovered that this syntax work too:
Copy code
with_options only_on: :index do |only_index|
    only_index.field :id, as: :id, visible: -> (resource:) { context[:user].is_admin?}
  end
a
oh interesting
that's a reasonable syntax, I actually like that it's a little more verbose
making it explicit where the field goes
l
Actually I found it interesting too, of course you can rename the block arg
only_index
to whatever fits