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

  • we added <%= @temperature %> instead of {@temperature} elixirliveview




    defmodule HelloWeb.ThermostatLive do use HelloWeb, :live_view

    def render(assigns) do ~H""" Current temperature: <%= @temperature %> + """ end

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

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


    !!! somewhat the buttons are not showing up in the code.!!!