What is the way to handle associations and nested forms in Phoenix framework? How would one create a form with nested attributes? How would one handle it in the controller and model?
相关问题
- Convert a map into a keyword list in Elixir
- Understanding pattern matching in Elixir function
- What does “&1” mean in elixir function?
- Preload all Relationships
- How to select id with max date group by category i
相关文章
- Macro expansion in elixir: how to define 2 macros
- Chunking list based on struct type changing
- How to get data from Ecto in a custom mix task
- Elixir case on a single line
- How do you embed double-quotes an Elixir string?
- How To Get The Root Directory Of an Elixir Project
- How can I make Elixir mix test output more verbose
- elixir Logger for lists, tuples, etc
Ran into the same problem with a
has_many
relationship. Unfortunately, aCar
cannot have manyEngines
, so I'd take the same example in this blogpost, of aTodoList
, with manyTodoItems
TodoList
model:TodoItem
model:Here is the form creation a
TodoList
. To keep things simple let's just add one item for now.This is how
TodoListController
would look like. Thecreate
method was the trickiest to get right. I had to dig into Ecto Tests to find a way to make this work. Linkhttp://pranavsingh.co/storing-nested-associations-with-phoenix-forms/
There is a simple example of handling 1-1 situation.
Imagine we have a
Car
and anEngine
models and obviously aCar
has_oneEngine
. So there's code for the car modeland the engine model
Simple template for the form ->
and the controller ->
and an interesting blog post on the subject that can clarify some things as well -> here