Embedded changesets: an `inputs_for` with checkboxes does not appear

Problem: Nothing appears between the two h3 tags:

<%= form_for @changeset, @action, fn f -> %>
  ....
  <h3>Permissions</h3>

  <% inputs_for f, :permission_list, fn p -> %>
    <%= label p, :manage_and_create_users %>
    <%= checkbox p, :manage_and_create_users %>

    <%= label p, :view_reservations %>
    <%= checkbox p, :view_reservations %>

  <% end %>


  <h3>END Permissions</h3>

No output from within the form_for appears. That’s true even if I plop a <p>THIS DOES NOT APPEAR</p> between the two checkboxes.

I’ve tried various solutions for embedded forms, like http://lesseverything.com/blog/nested-forms-in-phoenix/ and https://stackoverflow.com/questions/32054561/how-to-handle-associations-and-nested-forms-in-phoenix-framework but nothing seems to work. No doubt it’s something obvious.

The relevant schemas are:

  schema "users" do
    ...
    has_one :permission_list, PermissionList
    ...
  end

  schema "permission_lists" do
    belongs_to :user, Crit.Users.User
    field :manage_and_create_users, :boolean, default: false
    ...
    field :view_reservations, :boolean, default: true
  end

I’ve passed various shapes of changesets to the template. Here, for example, is one I patterned after the solution in a blog post. It looks unlikely to be right, but I’ve tried various combinations of putting things in changeset.changes and changeset.data. What I’d like to know is: what should a changeset look like to achieve the desired effect?

The changeset: 

#Ecto.Changeset<action: nil, changes: %{}, errors: [], data: #Crit.Users.User<>,
 valid?: true>

changeset.data:

%Crit.Users.User{
  __meta__: #Ecto.Schema.Metadata<:built, "users">,
  active: true,
  auth_id: nil,
  display_name: nil,
  email: nil,
  id: nil,
  inserted_at: nil,
  password_token: #Ecto.Association.NotLoaded<association :password_token is not loaded>,
  permission_list: #Ecto.Changeset<action: nil, changes: %{}, errors: [],
   data: #Crit.Users.PermissionList<>, valid?: true>,
  updated_at: nil
}

That comes from this in the controller (but, like I say, this was a last flailing attempt):

  def new(conn, _params) do
    embedded_changeset = PermissionList.changeset(%PermissionList{})
    changeset = User.changeset(%User{permission_list: embedded_changeset})
    IO.inspect changeset
    IO.puts "988888888888888"
    IO.inspect changeset.data
    # IO.inspect to_string(changeset.changes.permission_list.data.manage_and_create_users)
    render(conn, "new.html", changeset: changeset)
  end

I think You miss an equal sign… like this

<%= inputs_for f, :permission_list, fn p -> %>
1 Like

Oh dear. Thank you. Grr.

1 Like

That happens to us all, more often than most of us want to admit. ^.^;

1 Like