Search code examples
elixirphoenix-live-view

Phoenix Liveview Sample App ThermostatLive not rendering variable correctly


I am trying to go through the tutorial app for Phoenix/Liveview.

Here are the versions used:

{:phoenix, "~> 1.7.14"},
{:phoenix_live_view, "~> 1.0.0-rc.1", override: true},

When I run the page, I get simply Current temperature: {@temperature}°F instead of Current temperature: 70°F. The code is 100% the same (except for the module name):

defmodule HelloWeb.ThermostatLive do
  use HelloWeb, :live_view

  def render(assigns) do
    ~H"""
    Current temperature: {@temperature}°F
    <button phx-click="inc_temperature">+</button>
    """
  end

  def mount(_params, _session, socket) do
    temperature = 70 # Let's assume a fixed temperature for now
    {:ok, assign(socket, :temperature, temperature)}
  end

  def handle_event("inc_temperature", _params, socket) do
    {:noreply, update(socket, :temperature, &(&1 + 1))}
  end
end

I do get phx-GBoxXiWqZC21mxGj error: view crashed - undefined in the dev-console, but I'm not sure whether this is something related to this problem.

Part of my router.ex:

scope "/", HelloWeb do
  pipe_through :browser
  get "/", PageController, :home
  #...
  live "/thermostat", ThermostatLive
end

Anything that is wrong?

btw. the example hello-messenger that renders a variable works


Solution

  • With help of: Peaceful James

    Answer: The error happens cause the use of {@temperature} in your version is not accurate. We will need to update it. By that mean use: mix deps.update where usually the mix.exs file is.


    As far as i know <%= @temperature %> will not be the default anymore.

    I will try to update this question whenever seems necessary. Ty all for the comments!