Getting "assign @room not available in eex template." on links that it is not passed to

I am rendering a navbar on my index page, passing it a room that is associated with the current logged in user.

<%= render "_navbar.html", conn: @conn, room: @room %>

And then I use @room.name to go to the room associated with the user by doing

<li><%= link "My Room", to: room_path(@conn, :show, @room.name), method: "get", class: "nav-link" %></li>

However, the All Users, Register, and Login links are now complaining about assign @room not available in eex template. when I try to go to them. Here is the full navbar.

<nav class="navbar navbar-expand-sm">
  <ul class="navbar-nav mr-auto">
    <li><%= link "Home", to: page_path(@conn, :index), class: "nav-link" %></li>
    <li><%= link "All Users", to: user_path(@conn, :index), class: "nav-link" %></li>
  </ul>  
  <ul class="navbar-nav">
  <%= if logged_in?(@conn) do %>
    <li><%= link "My Room", to: room_path(@conn, :show, @room.name), method: "get", class: "nav-link" %></li>
    <li><%= link "Logout", to: session_path(@conn, :delete), method: "delete", class: "nav-link" %></li>
  <% else %>
    <li><%= link "Register", to: user_path(@conn, :new), method: "get", class: "nav-link" %></li>
    <li><%= link "Login", to: session_path(@conn, :new), method: "get", class: "nav-link" %></li>
  <% end %>
  </ul>
</nav>

@room comes from controller. Navbar shows in every request. Every controllers of your app pass a conn to templates but not room.

1 Like