AE
Ash Elixir•2y ago
jasono

How to make 'actor' available in nested forms' action

I have Ash.set_actor(current_user) in the mount function of a liveview that contains a nested form. The create/update actions of the main (posts) and sub form (comments), both of which includes change fn changeset, %{actor: actor}, the variable actor passes the proper user map for post , but nil for comment`. Also tried passing actor like below, but it didn't help either. Is there anything else I need to do to make it available in the nested forms' action as well?
AshPhoenix.Form.for_update(post, :update,
api: MyPosts,
actor: socket.assigns.current_user,
forms: [

comments: [
resource: Comment,
data: [%Comment{}],
create_action: :create,
update_action: :update,
type: :list
],
AshPhoenix.Form.for_update(post, :update,
api: MyPosts,
actor: socket.assigns.current_user,
forms: [

comments: [
resource: Comment,
data: [%Comment{}],
create_action: :create,
update_action: :update,
type: :list
],
8 Replies
ZachDaniel
ZachDaniel•2y ago
🤔 interesting...that could be a bug potentially could you open an issue on ash_phoenix for this?
jasono
jasonoOP•2y ago
Will do. By the way, when I pass in actor like shown above in the for_update statement, and don't have Ash.set_actorin the mount function, actor is nil even in the main form (post)'s create action. That doesn't sound right, does it? I'm wondering if I'm missing something bovious.
ZachDaniel
ZachDaniel•2y ago
Yeah, I think I can see whats going on there. Basically the form itself needs to pick up the actor from context, not just leave it to the underlying changeset or query. The reason for this is that it explicitly sets that context for nested forms.
jasono
jasonoOP•2y ago
I see.
ZachDaniel
ZachDaniel•2y ago
If you provide the actor explicitly as an option I think that will be a workaround I won’t be able to fix in the next few days because I’m at ElixirConf
jasono
jasonoOP•2y ago
Lucky you. What do you mean by providing the actor as an option? Can you please elaborate a little bit?
ZachDaniel
ZachDaniel•2y ago
When creating a form, you can say actor: the actor
jasono
jasonoOP•2y ago
Thank you. It worked!.

Did you find this page helpful?