Nullstrike

Nullstrike

How to pass a changeset with put_assoc in Phoenix Form tag

How to pass a changeset in phoenix form?

I use put_assoc function as I get the user struct from the cureent user that are assign to session.

Here’s a snippet from the controller:

def new(conn, _params) do
    changeset = Blog.change_post(%Post{})
    render(conn, "new.html", changeset: changeset)
end

But I get this error
key :user not found in: %{}

Here’s my post changeset:

def changeset(post, attrs) do
   post
   |> cast(attrs, [:title, :content, :is_published])
   |> validate_required([:title, :content])
   |> put_slug()
   |> put_assoc(:user, attrs.user)
end

Marked As Solved

idi527

idi527

:waving_hand:

It seems like you are passing an empty attrs map in Blog.change_post(%Post{}) (the contents of the change_post/1 might be helpful), and in your changeset/2 you expect it to have a :user.

To avoid the error, try replacing your current change_post/1 with

def change_post(post) do
  Ecto.Changeset.change(post)
end

then the changeset/2 won’t be called.

Also Liked

chensan

chensan

Since you are using Blog context in your code, I would suggest putting put_assoc in your context instead of messing your changeset/2, for example, you can define change_post/2 as bellow:

def change_post(%User{} = user, %Post{} = post) do
  Post.changeset(post, %{})
  |> put_assoc(:user, user)
end

Then in your new action:

def new(conn, _) do
  changeset = Blog.change_post(conn.assigns.current_user, %Post{})
  render(conn, "new.html", changeset: changeset)
end
Nullstrike

Nullstrike

Yes it’s already been solved, I just forgot to marked this a solved.

Where Next?

Popular in Questions Top

chokchit
** (DBConnection.ConnectionError) connection not available and request was dropped from queue after 2733ms. You can configure how long re...
New
nobody
How to bind a phoenix app to a specific ip address? could not find anything about that, nowhere, unfortunately, but for me this is quite...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
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
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
yawaramin
In the Dialyzer docs ( dialyzer — OTP 29.0.2 (dialyzer 6.0.1) ), there is a way to turn off a specific warning for a function: -dialyzer...
New
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

Other popular topics Top

senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
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
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
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement