"assign @conn not available" error in simple login page

I’m working through a tutorial on phoenix authentication (https://goo.gl/yCvd37), actually my 6th or 7th such, but I keep running into the exact same error no matter what I do. At first I thought it might be a problem with using newer phoenix, but I think now that there is something that I have set incorrectly somewhere.

The issue is that I create a very basic login page, but I always get the error:

assign @conn not available in eex template.

and I think the relevant information says Available assigns: [:action].

Here is the new function from my controller:

def new(conn, _params) do render conn, "new.html" end

and my view:

defmodule Myapp.SessionView do use Myapp.Web, :view end

and my template for new.html.eex:

<%= render "form.html", action: session_path(@conn, :create) %>

and (I think) the relevant part of my template for form.html.eex:

<%= form_for @conn, session_path(@conn, :create), [as: :session], fn f -> %> ... just a basic login form here <% end %>

It really seems like this should work since it’s the same info I find over and over, so please let me know if I’m missing something simple. Thanks!

You also need to pass your connection as an assign if you want to use it in your view.
Phoenix.Controller.View.render/3 takes a connection, the template name and a Keyword list with the assigns that are supposed to be available in your view.
So, if you want to have your connection there, you need to call render/3 like this:

render conn, "new.html", conn: conn

Thanks for the quick response. However, I’ve tried that before (and again just now) and it makes no difference. I get the same error and info.

You then also need to pass it to your other template:

<%= render "form.html", action: session_path(@conn, :create), conn: @conn %>

Thanks so much, it worked!

Incidentally, it seems I don’t need to use render/3 in the controller to get the expected behavior, just using

render conn, "new.html"

works fine.

1 Like