gpreston

gpreston

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?

Showing Posts 1 to 9

ityonemo

ityonemo

mount/2 is now mount/3, depending on your version of phoenix liveview.

gpreston

gpreston OP

Thank you! Idk if this would warrant a new thread but I have another question. Now that I can load the page, it seems like things are not updating. Here is the rest of my code:

...
  def render(assigns) do
    ~L"""
    <h1>Count: <%= @count %></h1>
    <button phx-click="increment">+</button>
    <button phx-click="decrement">-</button>
    """
  end

  def handle_event("increment", _value, socket) do
    count = socket.assigns.count + 1
    socket = assign(socket, :count, count)
    {:noreply, socket}
  end

  def handle_event("decrement", _value, socket) do
    count = socket.assigns.count - 1
    socket = assign(socket, :count, count)
    {:noreply, socket}
  end

I’ve set up my LiveView install according to the docs, and the docs also show handle_event/3 being used like this. When I open the network tab of the developer console, I don’t see anything when I click the buttons. Any idea what could be wrong?

ityonemo

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

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.

gpreston

gpreston OP

I indeed only see one ID flash. It’s strange that it can get the initial value set in mount, but further events don’t work…

My /js/app.js does come after csrf_meta_tag(). It is in the body, while CSRF is in the head. I think my whole of app.html.eex was just auto-generated and I never needed to touch it because everything the guide recommended was already there.

ityonemo

ityonemo

ah. Two last things to check: 1) check the JS dev panel to make sure there aren’t any strange things happpening. 2) I don’t know why but my liveview page just stopped working on firefox. Well no. It didn’t stop working, I just had to wait 5 minutes for the page to second-mount (and only in my staging environment, not in dev or in prod). No JS errors were thrown. I didn’t know how to make this reproducible for the liveview team to check up on, so I didn’t report the error, so I had to start using chrome. Lmk if this is something you see. I’m on FF 74.0

sfusato

sfusato

Please check the rendered html in the browser and make sure app.js is loaded. By what it looks like to me, I think there’s a high chance it’s not included. If you don’t see app.js there, then check @sreyansjain suggestion above.

brunosantanaa

brunosantanaa

Here I did it by doing this:
router.ex:

   pipeline: browser
     plug: accepts, ["html"]
     plug: fetch_session
     #plug: fetch_flash
     plug: fetch_live_flash
     plug: protect_from_forgery
     plug: put_secure_browser_headers
  + plug: put_root_layout, {Webcandy2LocalWeb.LayoutView,: app}

in the app.html.eex replaces:

- <main role = "main" class = "container <% = Enum.at (@ conn.path_info, 0)%>">
-   <p class = "alert alert-info" role = "alert"> <% = get_flash (@conn,: info)%> </p>
-   <p class = "alert alert-danger" role = "alert"> <% = get_flash (@conn,: error)%> </p>
-   <% = render @view_module, @view_template, assigns%>
- </main>
+ <%= @inner_content %>
johnoscott

johnoscott

this means the mount function has 3 params. this change worked for me :

    def mount(_session, _, socket) do
        socket = assign(socket, :count, 0)
        {:ok, socket}
    end

ie. add an underscore as the 2nd param.

if you followed along with James Moore’s excellent intro video on YouTube - Phoenix LiveView for web developers who don’t know Elixir. then the change above is what you need to make the latest version of Phoenix work AND you should have created the initial project with the --live option.

$ mix phx.new counter --no-ecto --live
— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
apz
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews