Hello! I’ve been working with phoenix and liveview and am working through the programming liveview book. At the end of the first chapter you expand on a guessing game concept that you touch on and are challenged to make it function properly, as the chapter only has the user losing. I’ve got the user’s guess coming across the line fine, but not the value set in the mount and I’m not sure where I’m going wrong. I can see the value itself in the socket
My LiveView looks like this:
defmodule PracticeWeb.GuessLive do
use PracticeWeb, :live_view
@impl Phoenix.LiveView
def mount(_params, _session, socket) do
{:ok,
socket
|> assign(
score: 0,
message: "Can you guess the right number?")
|> assign_new(:value, fn -> Enum.random(1..10) end)
|> IO.inspect(label: :mount_values)
}
end
@spec render(any) :: Phoenix.LiveView.Rendered.t()
@impl Phoenix.LiveView
def render(assigns) do
~H"""
<.section
title="Guess the right number">
<h2><%= @message %></h2>
<%= @value %>
<div class="w-full inline-grid grid-cols-1 sm:grid-cols-3 gap-4 my-4">
<%= for n <- 1..10 do %>
<.button class="btn-sm w-full sm:w-48" phx-click="users_guess" phx-value-number={n}>
<%= n %>
</.button>
<% end %>
</div>
<p>Your score: <span class="bold"><%= @score %></span></p>
</.section>
"""
end
@impl Phoenix.LiveView
def handle_event(
"users_guess",
%{"number" => guess},
socket)
do
case guess == @value do
true ->
message = "You guessed #{guess}. Correct! Can you guess correctly again?"
score = socket.assigns.score + 1
{:noreply,
socket
|> assign( message: message, score: score)
|> assign_new(:value, fn -> Enum.random(1..10) end)}
false ->
message = "You guessed #{guess}. Wrong! Guess again."
score = socket.assigns.score - 1
{:noreply,
socket
|> assign( message: message, score: score)
|> IO.inspect(label: :socket_wrong)}
end
end
end
And the output is looking like this:
[debug] Processing with PracticeWeb.GuessLive.index/2
Parameters: %{}
Pipelines: [:browser]
mount_values: #Phoenix.LiveView.Socket<
id: "phx-F1scb7yHrlYhXhyC",
endpoint: PracticeWeb.Endpoint,
view: PracticeWeb.GuessLive,
parent_pid: nil,
root_pid: nil,
router: PracticeWeb.Router,
assigns: %{
__changed__: %{message: true, score: true, value: true},
flash: %{},
live_action: :index,
message: "Can you guess the right number?",
score: 0,
value: 1
},
transport_pid: nil,
...
>
[info] Sent 200 in 4ms
[info] CONNECTED TO Phoenix.LiveView.Socket in 31µs
Transport: :websocket
Serializer: Phoenix.Socket.V2.JSONSerializer
Parameters: %{"_csrf_token" => "bT4xPFkZCDsAcyQ9dS89IBAyAxUuOgYR8xbQka8uOGhUBiKjgpnPaxrF", "_live_referer" => "undefined", "_mounts" => "0", "_track_static" => %{"0" => "http://localhost:4000/assets/app.css", "1" => "http://localhost:4000/assets/app.js"}, "vsn" => "2.0.0"}
[debug] MOUNT PracticeWeb.GuessLive
Parameters: %{}
Session: %{"_csrf_token" => "UFSm2x0NO4Lh7FvJwBmEOBtW"}
mount_values: #Phoenix.LiveView.Socket<
id: "phx-F1scb7yHrlYhXhyC",
endpoint: PracticeWeb.Endpoint,
view: PracticeWeb.GuessLive,
parent_pid: nil,
root_pid: #PID<0.2894.0>,
router: PracticeWeb.Router,
assigns: %{
__changed__: %{message: true, score: true, value: true},
flash: %{},
live_action: :index,
message: "Can you guess the right number?",
score: 0,
value: 3
},
transport_pid: #PID<0.2888.0>,
...
>
[debug] Replied in 671µs
[debug] HANDLE EVENT
View: PracticeWeb.GuessLive
Event: "users_guess"
Parameters: %{"number" => "6", "value" => ""}
socket_wrong: #Phoenix.LiveView.Socket<
id: "phx-F1scb4bEH4KG5xtC",
endpoint: PracticeWeb.Endpoint,
view: PracticeWeb.GuessLive,
parent_pid: nil,
root_pid: #PID<0.2873.0>,
router: PracticeWeb.Router,
assigns: %{
__changed__: %{message: true, score: true},
flash: %{},
live_action: :index,
message: "You guessed 6. Wrong! Guess again.",
score: -1,
value: 6
},
transport_pid: #PID<0.2867.0>,
...
>
[debug] Replied in 1ms
Thanks for any help or guidance!