I'm using the amazing Phoenix Web Framework and trying to figure out how to create the form field(s) for a model with an array field.
Here is an example field from the model:
field :grateful, {:array, :string}
I've tried generating the fields like this:
<%= inputs_for f, :grateful, fn fp -> %>
<%= text_input fp, :grateful %>
<% end %>
But I get this error: could not generate inputs for :grateful from Motivation.DailyPost. Check the field exists and it is one of embeds_one, embeds_many, has_one, has_many, belongs_to or many_to_many
If I generate the field like this: <%= text_input fp, :grateful %> it generates a form field with a name of: daily_post[grateful] which actually won't work. I would need daily_post[grateful][].
The code below works, but loading the data after saving does not work. All array values are merged into one input field.
<div class="form-group" id="grateful-group">
<%= label f, :grateful, class: "control-label" %>
<%= text_input f, :grateful, name: "daily_post[grateful][]" %>
<%= error_tag f, :grateful %>
<input type="button" class="btn btn-success" id="add-grateful" value="add" />
<script>
window.onload = () => {
$('#add-grateful').click((e) => {
$('<input type="text" name="daily_post[grateful][]" />').appendTo("#grateful-group");
})
}
</script>
</div>
How can I properly work with the array datatype in phoenix?
Thanks!