wolfiton

wolfiton

Hi everyone,

Problem in programming phoenix page 55

Error trace:

[info] GET /users/
[debug] Processing with RumblWeb.UserController.index/2
  Parameters: %{}
  Pipelines: [:browser]
[info] Sent 500 in 53ms
[error] #PID<0.435.0> running RumblWeb.Endpoint (connection #PID<0.434.0>, stream id 1) terminated
Server: localhost:4000 (http)
Request: GET /users/
** (exit) an exception was raised:
    ** (ArgumentError) assign @user 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: [:conn, :users, :view_module, :view_template]

        (phoenix_html) lib/phoenix_html/engine.ex:133: Phoenix.HTML.Engine.fetch_assign!/2
        (rumbl) lib/rumbl_web/templates/user/index.html.eex:2: RumblWeb.UserView."index.html"/1
        (rumbl) lib/rumbl_web/templates/layout/app.html.eex:26: RumblWeb.LayoutView."app.html"/1
        (phoenix) lib/phoenix/view.ex:410: Phoenix.View.render_to_iodata/3
        (phoenix) lib/phoenix/controller.ex:729: Phoenix.Controller.__put_render__/5
        (phoenix) lib/phoenix/controller.ex:746: Phoenix.Controller.instrument_render_and_send/4
        (rumbl) lib/rumbl_web/controllers/user_controller.ex:1: RumblWeb.UserController.action/2
        (rumbl) lib/rumbl_web/controllers/user_controller.ex:1: RumblWeb.UserController.phoenix_controller_pipeline/2
        (phoenix) lib/phoenix/router.ex:288: Phoenix.Router.__call__/2
        (rumbl) lib/rumbl_web/endpoint.ex:1: RumblWeb.Endpoint.plug_builder_call/2
        (rumbl) lib/plug/debugger.ex:122: RumblWeb.Endpoint."call (overridable 3)"/2
        (rumbl) lib/rumbl_web/endpoint.ex:1: RumblWeb.Endpoint.call/2
        (phoenix) lib/phoenix/endpoint/cowboy2_handler.ex:42: Phoenix.Endpoint.Cowboy2Handler.init/4
        (cowboy) /home/dan/Codes/rumbl/deps/cowboy/src/cowboy_handler.erl:41: :cowboy_handler.execute/2
        (cowboy) /home/dan/Codes/rumbl/deps/cowboy/src/cowboy_stream_h.erl:320: :cowboy_stream_h.execute/3
        (cowboy) /home/dan/Codes/rumbl/deps/cowboy/src/cowboy_stream_h.erl:302: :cowboy_stream_h.request_process/3
        (stdlib) proc_lib.erl:249: :proc_lib.init_p_do_apply/3

I know that this errors tell me that I don’t have assigned to the template the value of user but i did using this:

Content from :user/index.html.eex

<tr>
<td><%= render("user.html", user: @user) %></td>
<td><%= link "View", to: Routes.user_path(@conn, :show, @user.id) %></td>
</tr>

Content from :user/show.html.eex

<h1>Showing User</h1>
<%= render "user.html", user: @user %>

Content from: user/user.html.eex
<strong><%= first_name(@user) %></strong> (<%= @user.id %>)

So why does phoenix tell me that i haven’t assigned user in the template?

Thanks in advance

Showing Posts 1 to 10

NobbZ

NobbZ

How does this function look like? Do you have any calls to render/* there?

wolfiton

wolfiton OP

My UserController content:

defmodule RumblWeb.UserController do
  use RumblWeb, :controller

  alias Rumbl.Accounts

  def index(conn, _params) do
    users = Accounts.list_users()
    render(conn, "index.html", users: users)
  end

  def show(conn, %{"id" => id}) do
    user = Accounts.get_user(id)
    render(conn, "show.html", user: user)
  end
end

Also this is the UserView content:

defmodule RumblWeb.UserView do
  use RumblWeb, :view

  alias Rumbl.Accounts

  def first_name(%Accounts.User{name: name}) do
    name
    |> String.split(" ")
    |> Enum.at(0)
  end
end
NobbZ

NobbZ

:users is not @user, your index.html.eex is wrong.

wolfiton

wolfiton OP

OK the book is also wrong about that one. Because the content is like this for index.html.eex

<tr>
<td><%= render "user.html", user: user %></td>
<td><%= link "View", to: Routes.user_path(@conn, :show, user.id) %></td>
</tr>
NobbZ

NobbZ

Is it just that? I’d expect a for around that… Something like this:

<table>
<%= for user <- @users do %>
  <tr><td><%= render "user.html", user: user %></td></tr>
<% end %>
</table>
wolfiton

wolfiton OP

It uses the user.html.eex to get rid of duplication but to me it seems that it doesn’t work

The following code for index.html.eex doesn’t work but it compiles:

<tr>
<td><%= render("user.html", users: @users) %></td>
<td><%= link "View", to: Routes.user_path(@conn, :show, @users.id) %></td>
</tr>

The following doesn’t compile:

<tr>
<td><%= render("user.html", users: users) %></td>
<td><%= link "View", to: Routes.user_path(@conn, :show, users.id) %></td>
</tr>
NobbZ

NobbZ

user.html.eex is for a single user, but index (by convention) is for many. So you need to iterate over your many users and render them one by one.

I do not have access to the book. I can only tell you what I think is going on here.

wolfiton

wolfiton OP

This is the excerpt of the part we are talking about:

Nesting Templates
Often there’s a need to reduce duplication in the templates themselves. For ex-
ample, both of our templates have common code that renders a user. Take the
common code and 
create a user template in lib/rumbl_web/templates/user/user.html.eex:
<strong><%= first_name(@user) %></strong> (<%= @user.id %>)
We created another template to render a user. Then, whenever we build tables
or listings of users, we can re-use this template. Now, change your show.html.eex
template to render it:
<h1>Showing User</h1>
<%= render "user.html", user: @user %>
Also, change your index.html.eex template to render it:
<tr>
<td><%= render "user.html", user: user %></td>
<td><%= link "View", to: Routes.user_path(@conn, :show, user.id) %></td>
</tr>

NobbZ

NobbZ

Yes, "user.html.eex" seems to be fine. As I said already… There is a loop missing in index.html.eex.

I’m pretty sure, the book already had a loop and wants you to replace the loops body with the snippet you show, while you replaced the full content.

wolfiton

wolfiton OP

That makes sense I will do that the original loop is this one:

Or here https://media.pragprog.com/titles/phoenix14/code/controllers_views_templates/listings/rumbl/lib/rumbl_web/templates/user/index.html.eex

<h1>Listing Users</h1>
<table>
<%= for user <- @users do %>
<tr>
<td><b><%= first_name(user) %></b> (<%= user.id %>)</td>
<td><%= link "View", to: Routes.user_path(@conn, :show, user.id) %></td>
</tr>
<% end %>
</table>

Where Next? Top

Trending in Chat/Questions Top

Other Trending Topics Top

garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
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
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews