Phoenix - Edit Associated Data

I am building an application with auth and basic user profiles. Currently I have users and profiles stored in separate tables, with a user_id reference on the profiles table. In the schemas, each user has_one profile and each profile belongs_to a user. Accounts.get_user/1 preloads the Profile and I understand how to display the profile data in the user/:id route. So far so good.

I’m struggling to create a form that can edit a user and their profile at once. In form.html.eex, IO.inspect f.data.profiles shows the preloaded profile. Can someone point me to a good resource that explains how to edit multiple structs in a form? I’m lost in the docs.

https://hexdocs.pm/phoenix_html/Phoenix.HTML.Form.html and look for inputs_for.

Roughly you’re looking for something like this

form_for blah fn f ->
  text_input f, :blah
  inputs_for f, :profile, fn p ->
    text_input p, :name 
  end
end
3 Likes

That’s exactly what I needed. Thank you.