Wigny

Wigny

Saving many entries from a form at once

Hey folks,

I have a problem that does not seem to have a straightforward solution to me, but it feels like it should have one:
How should we go about saving a form (adding/removing/updating entries) that produces a list of changesets, makes the operations in the DB and then refreshes the form with the new values?

I know that is possible when the form is based on a schema that has an embeds_many or has_many, as we have a guide in LiveView docs and also the excellent One-to-Many LiveView Form | Benjamin Milde article from @lostkobrakai, but given I would like to change multiples entries that doesn’t belongs_to a unique schema, how can I put this together inside a single form?

This question comes from a page that I’m trying to build that should allow the user to add a new entry in the DB, update an existing one or delete it, like in a bulk operation. Also, the table should have constraints, and therefore I thought using changesets would make it easy to handle and return error messages for inputs.

I’ve put a gist that goes into the direction of what I want, but I’m stuck in saving the list of changesets and refreshing the form based on this list:

How would you tackle that? Would you simply not use form to solve this problem?

First Post!

LostKobrakai

LostKobrakai

Why do you think you cannot create one? It could even live just in the controller/live view rendering your form.

Last Post!

Wigny

Wigny

Yes, I have. I think the form wasn’t behaving correctly when I tried it because I was calling put_embed after the changesets being applied to the DB, which caused them to have the action replace and it left the form in a wrong state.

I guess I found a workaround for that, which is to reinitialise the original changeset instead of trying to update it after applying the operations to the DB. The code is something like this now:

def save(changeset) do
  emails_changesets = get_embed(changeset, :emails)

  case apply_changesets(emails_changesets) do
    {:ok, emails_list} -> {:ok, MailingList.changeset(%MailingList{emails: emails_list})}
    {:error, emails_changesets} -> {:error, put_embed(changeset, :emails, emails_changesets)}
  end
end

Where Next?

Popular in Questions Top

vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New

Other popular topics Top

baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 49084 226
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 42533 114
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement