Issue With Data Sent from Form

The data loads correctly, but when I click on submit, the data is received (I can see it in the debug shell). However phoenix show me this error: assign @entities not available in eex template.

This is my frontend code (index.html), where the user can create a new asset:

  <%= form_for @conn, asset_path(@conn, :create), [as: "new_asset"], fn f -> %>
    <label>Entity Name: <%= select(f, :entity_id, @entities |> Enum.map(fn(e) -> ["#{e.name}": e.id] end) |> List.flatten) %></label></br></br>
    <%= submit "Submit" %>
  <% end %>

And this my controller:

    def index(conn, _params) do
      entities = Repo.all(Entity)
      render(conn, "index.html", entities: entities)
  end

  def create(conn, _params) do
    newAssetData = conn.params["new_asset"]
    IO.inspect newAssetData
    render(conn, "index.html")
  end

You’re rendering “index.html” in your create/2 but without assigning entities.

Thanks for the answer @ryh, but I don’t understand what you mean. Can you elaborate further?

entities = Repo.all(Entity)
render(conn, "index.html", entities: entities)

Should also be in your create/2 (or you can redirect back to index).

1 Like

Oh! I got my mistake. Thanks!