A small collection of functions to make it easier working with many_to_many Ecto
associations and checkboxes to create them.
If you are familiar with Ruby on Rails, analogous to collection_check_boxes.
Add :phoenix_mtm to your list of dependencies in mix.exs:
def deps do
[
{:phoenix_mtm, "~> 1.0.0"}
]
endIf you're still on Ecto 2, you'll want to use the pre-1.0 release:
def deps do
[
{:phoenix_mtm, "~> 0.5.1"}
]
end-
Ensure your schema is setup with a
many_to_manyassociation. You will likely need to ensure theon_deleteandon_replacekeys are present. See the example in the docs forPhoenixMTM.Changeset.cast_collection. -
Inside your changeset function, pipe your changeset through
PhoenixMTM.Changeset.cast_collection, providing the association name, repo module, and association module. -
Inside your template, call
PhoenixMTM.Helpers.collection_checkboxeswhere you want the output of checkboxes to occur. This function accepts a form, the association name, a keyword list of values, and a list of pre-selected values.You can pass along attributes directly to the generated inputs and labels by passing a keyword list inside both
label_optsandinput_optskeys.
I love pull requests! If you fork this project and modify it, please ping me to see if your changes can be incorporated back into this project.
That said, if your feature idea is nontrivial, you should probably open an issue to discuss it before attempting a pull request.
Copyright (c) 2016 Adam Daniels
This work is free. You can redistribute it and/or modify it under the terms of the MIT License. See the LICENSE.md file for more details.