[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>
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
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>
<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>
<table>
<%= for user <- @users do %>
<tr>
<td><%= render("user.html", users: @users) %></td>
<td><%= link "View", to: Routes.user_path(@conn, :show, @users.id) %></td>
</tr>
<% end %>
</table>
Error trace:
[info] GET /users/
[debug] Processing with RumblWeb.UserController.index/2
Parameters: %{}
Pipelines: [:browser]
[info] Sent 500 in 52ms
[error] #PID<0.459.0> running RumblWeb.Endpoint (connection #PID<0.458.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: [:users]
(phoenix_html) lib/phoenix_html/engine.ex:133: Phoenix.HTML.Engine.fetch_assign!/2
(rumbl) lib/rumbl_web/templates/user/user.html.eex:1: RumblWeb.UserView."user.html"/1
(rumbl) lib/rumbl_web/templates/user/index.html.eex:4: anonymous fn/3 in RumblWeb.UserView."index.html"/1
(elixir) lib/enum.ex:1948: Enum."-reduce/3-lists^foldl/2-0-"/3
(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
<table>
<%= for user <- @users do %>
<tr>
<td><%= render("user.html", user: user) %></td>
<td><%= link "View", to: Routes.user_path(@conn, :show, user.id) %></td>
</tr>
<% end %>
</table>