Errors with Phoenix Live View

I’m trying to test out the new LiveView functionality. I have two questions:

First, when testing out the “LiveUser” functionality from the sample repo in my own project, I get that conn is not available in my template:

ArgumentError at GET /users/new
assign @conn 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: [:changeset, :count, :session, :socket]

Second, how can I redirect to a non-live view from a live view. For example, my UserLive.New wants to redirect to the non-live resources route. Where can I find conn for the redirect here?

  def handle_event("save", %{"user" => user_params}, socket) do
    case Authentication.create_user(user_params) do
      {:ok, _user} ->
        {
          :stop,
          socket
          |> put_flash(:info, "user created")
          |> redirect(to: Routes.user_path(conn, :index)) # where does conn come from?
        }

      {:error, %Ecto.Changeset{} = changeset} ->
        {:noreply, assign(socket, changeset: changeset)}
    end
  end
2 Likes

As far as I can tell conn and socket are interchangeable here. However, when I try the new form using @socket instead of @conn, I’m seeing a physical POST to /users/new#. It looks like phx_disable_with is not working in my form.html.leex.

Edit:

Hmm, it seems like none of my CSS or Javascript is loaded on the page. Do I need a separate app.html.leex here? curling localhost:4000 yields only the leex fragment and not any of app.html.eex. You can find my project here: https://github.com/danieljaouen/testing_live_view if any of you want to look over the code.

Edit 2:

Figured it out. I was missing the :put_layout plug in my router. Problem solved.

3 Likes