How do I access embedded field inside a form?

Hi,

I have a json column inside my database, it contains only one object so I’m using embeds_one in my schema. i also have a form as part of the template but I don’t know how to access the fields to create/edit this object.

<%= for song <- @album.cover_song do %>
      <div class="">
        <%= label song, :title %>
        <%= text_input song, :title %>
        <%= error_tag song, :title %>
      </div>
<% end %>

With this code I’m getting " protocol Enumerable not implemented…" error which is understandable as there is only one map here and it will always be just one, this code works fine with embeds_many when there are multiple maps inside the list.

So, how do I access those fields in my template when there is just one embedded map?

See docs on nested inputs: Phoenix.HTML.Form — Phoenix.HTML v3.2.0

Probably something like this in your case:

<%= inputs_for f, :cover_song, fn fcs -> %>
  <%= label fcs, :title %>
  <%= text_input fcs, :title %>
  <%= error_tag fcs, :title %>
<% end %>
1 Like

I don’t know how I missed this… thanks a lot!