I wrote this sample project to get more familiar with LiveView, but it gives me this error on going to the live route
assign @temp not available in eex template.
here is my code
defmodule ThermoWeb.ThermoLive do
use Phoenix.LiveView
alias ThermoWeb.ThermoView
def mount(_params, socket) do
{:ok, assign(socket, :temp, 32)}
end
def render(assigns) do
~L"""
Current temperature: <%= @temp %>
<button phx-click="set_temp">Set</button>
"""
end
def handle_event("set_temp", socket) do
{:noreply, assign(socket, :temp, 24)}
end
end
The mount
callback expects 3 arguments. Try:
def mount(_params, _session, socket) do
{:ok, assign(socket, :temp, 32)}
end
1 Like
Thank you! The view now gets mounted but whenever I click on the button it does nothing. any help?
Oh right. handle_event
also expects 3 arguments. Try:
def handle_event("set_temp", _params, socket) do
{:noreply, assign(socket, :temp, 24)}
end
1 Like
I already did that before asking you . Unfortunately, it still does not work, the event is not being fired I think!
I can’t see anything else that might be wrong. Can you provide more details?
1 Like
yes sure, what other parts of code you wanna see? After configuring LiveView following this I did not write anything except this and the route
live "/thermo", ThermoLive
in router.ex
.
Have you edited your app.js
accordingly?
yes, I did. I cloned the example repo and it works fine. what could be wrong with my code?
Try opening your browser’s Developer Tools (usually Ctrl+Shift+I) and see what errors are there.