gpreston
LiveView: (ArgumentError) assign @count not available in eex template
I have a LiveView with the following code:
defmodule Webcandy2LocalWeb.CounterLive do
use Phoenix.LiveView
def mount(_session, socket) do
socket = assign(socket, :count, 0)
{:ok, socket}
end
def render(assigns) do
~L"""
<h1>Count: <%= @count %></h1>
<button>+</button>
<button>-</button>
"""
end
end
When I try to access the page for this LiveView, I get the following error:
Request: GET /counter
** (exit) an exception was raised:
** (ArgumentError) assign @count not available in eex template.
Please make sure all proper assigns have been set. If this
is a child template, ensure assigns are given explicitly by
the parent template as they are not automatically forwarded.
Available assigns: [:flash, :live_action, :live_module, :socket]
I don’t see why this is happening because I assign :count in the mount/2 function. Is there something I’m missing?
Marked As Solved
ityonemo
Also Liked
ityonemo
ok i’m flying totally in the dark here, so huge props to me if I get this right, lol.
seems like your liveview is mounting only over html and not over the socket. Check to make sure your /js/app.js is after your crsf_meta_tag() function call in the app.html.eex. And it might help? (probably not), to have that be after the render call. The recommended places to put these swapped not to long ago IIRC.
You can generally check to see if it’s mounted by placing some sort of sentinel inside the “mount” call. Usually I like to self() |> IO.inspect(label: "foo") and watch two distinct PIDs fly by in my console when I mount. If you see only one, it’s a sure sign that the websocket (second) mount didn’t happen.
Also for things that are not customer-facing (like operator dashboards) it’s good to put a sentinel inside of your assigns, I have a @live assign which is true when on the second mount, that makes it say [live] or [not live] as expected. Liveness can be ascertained using the connected?/1 function inside your mount.
sreyansjain
Which version of live view are you using. If you are using v0.10.0 or above please check this
Live view layouts have changed and it might so happen that your app.js is not being called at all.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #hex









