Adrian I m trying to use Avo for regular
# avo-2
a
Adrian, I'm trying to use Avo for regular users too. Is there a way to use some equivalent of 'current_user.resource.new/create'? All I see in the docs is admin focused where I could choose the User/Account/Whatever association with a dropdown box. I assume this is here, I just don't see it in the docs.
l
hey man
don't understand what
current_user.resource.new/create
means
can you please elaborate?
a
sure
so historically (in my old Rails apps) if I might call 'current_user.blogposts.new' or something when a user clicks a 'Create Blog Post' button
In an admin interface, you don't assume who created said blog post, so Avo gives me a dropdown to choose which User a new blog post belongs to
(which is awesome behavior)
l
oh, you mean to set a default value for a field?
a
I'm trying to use Avo for non-admin users too, since it's so clean. But non-admin users obviously would be always set to a specific Blog Post owner
l
field :user, as: :belongs_to, default -> { current_user.id }
a
I can do a default value easy enough in the Resource
l
you could add a a
disabled: -> { current_user.blank? || !current_user.is_admin? }
if you 'd like that field disabled
a
I'm wondering if that's the correct way, or if there's one that would involve current_user.association.create/new
l
or... you can go deeper and add ahidden field
field :user_id, as: :hidden, default -> { current_user.id }
I don't know what this is
current_user.association.create/new
😂
is that a variable, a method call?
a
you've never used that syntax in a controller?
l
no
please show me
a
sure
this is from one of my old Rails 5 apps: def new @organization = current_user.organizations.new render partial: "new" end
l
oh. ok. the
/new
part threw me off
a
def create @organization = current_user.organizations.new(organization_params) save_model(@organization, "Your organization has been saved.") end
(save_model is a custom thing that saves the new object and does a few other things that Rails didn't do by default back when we started)
l
I think this should do exaclty that
field :user_id, as: :hidden, default -> { current_user.id }
a
this codebase is like 10 years old so maybe that isn't even the 'right way' to do it in modern rails
l
I think you can do it within Avo too
a
I'm all for minimal code so I'd like to stick as much to the 'Avo Way' when working inside Avo
l
this will get you there 100%
a
awesome
thanks