def handle_event("add_points", %{"user-id" => user_id}, socket) do
{:noreply, assign(socket, :user_id, user_id)}
end
def handle_event("add_points", %{"user_points" => %{"points" => points_for_user}}, socket) do
user_id = socket.assigns[:user_id]
post = socket.assigns[:post]
|> Repo.preload(:users)
Events.add_user_points(post, user_id, points_for_user)
{:noreply, socket}
end
The problem is that I get an ** (ArgumentError) comparison with nil is forbidden as it is unsafe. If you want to check if a value is nil, use is_nil/1 instead error when using the user_id in a query:
def add_player_points(%Post{} = post, user_id, points_for_user) do
pp = post.id
query = from(pu in PostUser, where: pu.post_id == ^pp and pu.user_id == ^user_id) #here
...
I have tried also this in one handle event:
def handle_event("add_points", %{"user_points" => %{"points" => points_for_user}}, socket) do
user_id = "user-id"
post = socket.assigns[:post]
|> Repo.preload(:users)
Events.add_user_points(post, user_id, points_for_user)
{:noreply, socket}
end
I’m so sorry, but I got really confused… As I understand, we cannot use phx-value in a form event? When doing
<%= hidden_input :user_id, :user_id %>
I got the message: ==========> MESSAGE: %{"user_id" => %{"user_id" => ""}, "user_points" => %{"points" => "2"}}, but how should I set up the value of that user_id? (eg. phx_value: user.id)?