I’ve got a schema called Project
and it has a field named subcategories
and that field is an array of strings in the DB (Postgres).
Schema I have:
#project.ex
...
schema "projects" do
field :subcategories, {:array, :string}
field(:name, :string)
timestamps()
end
...
For that reason in my project/edit.html.eex
form I would like to have checkboxes for a user to select subcategories and I am planning to keep those subcategories in the DB in my subcategories
column as an array of strings. I’ve tried to use inputs_for/2
but that didn’t really help.
I get this error below:
could not generate inputs for :subcategories from Projects.Project. Check the field exists and it is one of embeds_one, embeds_many, has_one, has_many, belongs_to or many_to_many
For this snippet from project/edit.html.eex
:
<div class="row">
<div class="col">
<div class="form-group">
<%= label f, :subcategories, "Choose subcategories"%>
<%= for checkbox_form <- inputs_for(f, :subcategories) do %>
<%= for subcategory <- @subcategories do %>
<div class="form-check">
<%= checkbox f, subcategory, class: "form-check-input"%>
<%= label f, subcategory, subcategory, class: "form-check-label" %>
</div>
<% end %>
<% end %>
</div>
</div>
</div>
After submitting the edit form what I expect to see in params is:
"project" => %{"name" => "foo", "subcategories" => %{"foo" => "true", "bar" => "false", "baz" => "true"}}
A similar question on SO though for reference.