Should phx.gen.html generate forms for references?


mix phx.gen.html Calendar Event events summary:string date:date owner:references:users

generates this template:

  <%= if @changeset.action do %>
    <div class="alert alert-danger">
      <p>Oops, something went wrong! Please check the errors below.</p>
  <% end %>

  <%= label f, :summary %>
  <%= text_input f, :summary %>
  <%= error_tag f, :summary %>

  <%= label f, :date %>
  <%= date_select f, :date %>
  <%= error_tag f, :date %>

    <%= submit "Save" %>
<% end %> 

Am I doing something wrong that there is no form field for the owner field? Or does scaffolding skip relations?

1 Like

Yep, you’ll have to do it yourself because the generator can’t know which field of the relation you want to show, or if it’s even a selection box, a list of radio buttons, or something else.


Yes, it does skip references:

  defp inputs(%Schema{} = schema) do, fn
      {_, {:references, _}} ->
        {nil, nil, nil}
      {key, :integer} ->
        {label(key), ~s(<%= number_input f, #{inspect(key)} %>), error(key)}