Help liveview (render)

I have a little problem …
As I don’t know much about liveview …
I wanted some help from you.

I want my liveview page, access another view, is …
Ex:
<%= live_render ProjectWeb.LayoutView, "navbar.html", Map.put(assigns, :action, Routes.page_path(@conn, :index)) %>

I want to add it this way.

    def render(assigns) do
      ~L"""
   <%= live_render ProjectWeb.LayoutView, "navbar.html", Map.put(assigns, :action, Routes.page_path(@conn, :index)) %>

      <h1 id="myHeader"><%= @deploy_step %></h1>
      <form phx-submit="domaincheck">

      <div class="input-group input-group-sm">
          <label>Domínio:&nbsp;&nbsp;</label>
          <input type="text" name="params" list="matches" placeholder="Domínio..." class="form-control">
          <span class="input-group-append">
            <button type="submit" class="btn btn-info btn-flat">Checar Domínio!</button>
          </span>
        </form>
      </div>
      """
    end

Erro:

[error] #PID<0.803.0> running ProjectWeb.Endpoint (connection #PID<0.801.0>, stream id 1) terminated
Server: localhost:4000 (http)
Request: GET /teste
** (exit) an exception was raised:
** (ArgumentError) 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: [:deploy_step, :params, :socket]

    (phoenix_live_view) lib/phoenix_live_view/engine.ex:672: Phoenix.LiveView.Engine.fetch_assign!/2
    (project) lib/project_web/live/live_request.ex:6: ProjectWeb.LiveRequest.render/1
    (phoenix_live_view) lib/phoenix_live_view/view.ex:193: Phoenix.LiveView.View.render/2
    (phoenix_live_view) lib/phoenix_live_view/view.ex:292: Phoenix.LiveView.View.static_render/3
    (phoenix_live_view) lib/phoenix_live_view/controller.ex:37: Phoenix.LiveView.Controller.live_render/3
    (phoenix) lib/phoenix/router.ex:288: Phoenix.Router.__call__/2
    (project) lib/project_web/endpoint.ex:1: ProjectWeb.Endpoint.plug_builder_call/2
    (project) lib/plug/debugger.ex:122: ProjectWeb.Endpoint."call (overridable 3)"/2
    (project) lib/project_web/endpoint.ex:1: ProjectWeb.Endpoint.call/2
    (phoenix) lib/phoenix/endpoint/cowboy2_handler.ex:40: Phoenix.Endpoint.Cowboy2Handler.init/2
    (cowboy) /home/katmandu/Project/elixir/project/deps/cowboy/src/cowboy_handler.erl:41: :cowboy_handler.execute/2
    (cowboy) /home/katmandu/Project/elixir/project/deps/cowboy/src/cowboy_stream_h.erl:296: :cowboy_stream_h.execute/3
    (cowboy) /home/katmandu/Project/elixir/project/deps/cowboy/src/cowboy_stream_h.erl:274: :cowboy_stream_h.request_process/3
    (stdlib) proc_lib.erl:249: :proc_lib.init_p_do_apply/3

How do I get this to work?

I don’t think You pass the conn, but You use it later… pass the conn as parameters, then use conn.assigns if You want.

Actually in liveview, I use @socket

outside liveview, I use @conn

When I add @socket
<%= live_render ProjectWeb.LayoutView, "navbar.html", Map.put(assigns, :action, Routes.page_path(@socket, :index)) %>

generates this route error

[error] #PID<0.1079.0> running ProjectWeb.Endpoint (connection #PID<0.1053.0>, stream id 3) terminated
Server: localhost:4000 (http)
Request: GET /teste
** (exit) an exception was raised:
** (UndefinedFunctionError) function Routes.page_path/2 is undefined (module Routes is not available)
Routes.page_path(%Phoenix.LiveView.Socket{

If you need to redirect to a path that requires “@conn” you can either add the conn to the socket and have it available in template (not recommended) or use YourApp.Endpoint instead of @conn
Routes.some_path(YourApp.Endpoint, :index)

1 Like