Elixir: Using append and prepend options with inputs_for
November 28, 2017The inputs_for method comes with two options for rendering the page with new elements as part of the collection you’re modifying (if it is a collection). For example, if you’re creating a to-do list that has a number of items, you could use the options like so:
<%= inputs_for f, :items, [append: [%App.Lists.Item{}], fn i -> %>
<%= text_input i, :description %>
<% end %>
If we already have items in the collection, inputs_for will iterate and fill the fields with the appropriate values, then append the new entry to the end, or in the case of prepend it will add the new entry to the beginning of the collection.