elt547

elt547

Is there an explanation of Map.put(:action, :insert) somewhere?

In a validate method on phx-change, it’s common to Map.put(:action, :insert) to the changeset. This is obviously used somehow by the form function. I’m having a bit of trouble deciphering it, as I can’t figure out where the action is received from the for map/struct. Probably missed it, my eyes are still adjusting to elixir.

What specifically does this do and are there more values that are used by the .form function?

Most Liked

zorn

zorn

Per the links @cpgo supplied, generally speaking, the Phoenix HTML functions will not display errors for a changeset unless it has an action value, which usually is something Ecto will apply as part of a bad insert or update. If you are doing other more manual things with the changeset, you need to assign the action yourself to have an outcome where the form presents the errors as expected.

I think it is a mistake that the code you are looking at is editing the map directly. You should probably lean on the apply_action/2 function to avoid making assumptions about the underlying Changeset map structure.

https://hexdocs.pm/ecto/Ecto.Changeset.html#apply_action/2

Hope this helps. I know it took me a bit to grok when I first started.

markquezada

markquezada

Just wanted to note here that the official docs say it’s ok to update directly:

Ecto automatically applies the action for you when you call Repo.insert/update/delete, but if you want to show errors manually you can also set the action yourself, either directly on the Ecto.Changeset struct field or by using Ecto.Changeset.apply_action/2 .

(Emphasis mine.)

Probably explains why the generators use this pattern in live views:

  defp validate_thing(socket, thing_params) do
    changeset =
      socket.assigns.thing
      |> Things.change(thing_params)
      |> Map.put(:action, :validate)

    assign(socket, :changeset, changeset)
  end
LostKobrakai

LostKobrakai

While I generally support your suggestion there‘s also nothing wrong with depending on the changeset structure, which is publicly documented and not going to change (in a breaking way) without a mayor version bump of ecto.

Where Next?

Popular in Questions Top

openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
Phillipp
Hey, I have a NanoPi-M3 and try to install Elixir on their Ubuntu image. I followed the Raspberry Pi installation instructions from the ...
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
mathew4509
I have a list say x = ["23gh", "56kh", "97mh"] I would like to pass each element to Val in each iteration. Say, in iteration 1 -------...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
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
sabri
Can someone explain the settings of pool_size of Ecto in config file? and what is the recommend size? Thanks
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3268 119930 1237
New
JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New

We're in Beta

About us Mission Statement