sure.
<div class=“stats”>
<i class=“material-icons”>access_time</i> You have <%= @conn.assigns.current_user.credits %> credits.
</div>
I assign :current_user using Plug in load_user.ex
def call(conn, _opts) do
case user_id = get_session(conn, :user_id) do
user_id when user_id != nil ->
user = Account.get_user_by_id(user_id)
assign(conn, :current_user, user)
user_id when is_nil(user_id) ->
conn
|> put_flash(:error, "You must sign in first")
|> redirect(to: Helpers.sign_in_path(conn, :new))
|> halt()
end
end
@conn.assigns.current_user
in router.ex
pipeline :dashboard do
plug TextingWeb.Plugs.LoadUser
plug TextingWeb.Plugs.DashboardLayout
plug TextingWeb.Plugs.FetchRecipients
end
Drab uses castrated version of conn, as it could be very heavy, especially when you keep a lot of data in assigns. See this issue for a reference. Drab creates it’s own version of conn, which by default copy only the few fields in private.
This behaviour is documented here. What you need is just to add something like :assigns => :current_user to :live_conn_pass_through config.
I know it is quite counter-intuitive, but I didn’t find any solution to change this, or at least tell user it is using the castrated conn version. Anyone?
Ok, still I need way more information. On which line of code the error occurs and what’s there? What exactly you’ve set as :live_conn_pass_through in the config? What is %Texting.Account.User{}?
That is tricky. I thought errors from <%= @conn.assigns.current_user.credits %>
So I comment out that lines but it got me same errors.
And after set :live_conn_pass_through, drab in other page gives me an same error. (it worked ok before set :live_conn_pass) and some other page it shows me errors like this
** (Protocol.UndefinedError) protocol Enumerable not implemented for %Texting.Account.User{}
long line here.......and
(elixir) /home/ubuntu/bob/tmp/6f24db0ee55f44e6b3c1cfcd7feddfd8/elixir/lib/elixir/lib/enum.ex:1: Enumerable.impl_for!/1
(elixir) /home/ubuntu/bob/tmp/6f24db0ee55f44e6b3c1cfcd7feddfd8/elixir/lib/elixir/lib/enum.ex:141: Enumerable.reduce/3
(elixir) lib/enum.ex:1911: Enum.reduce/3
(drab) lib/drab/live/assign.ex:71: anonymous fn/2 in Drab.Live.Assign.deep_merge_map/2
(stdlib) maps.erl:257: :maps.fold_1/3
(drab) lib/drab/live/assign.ex:71: anonymous fn/2 in Drab.Live.Assign.deep_merge_map/2
(stdlib) maps.erl:257: :maps.fold_1/3
(drab) lib/drab/live/assign.ex:33: Drab.Live.Assign.merge/2
(drab) lib/drab/live.ex:898: Drab.Live.apply_conn_merge/1
(elixir) lib/enum.ex:1298: anonymous fn/3 in Enum.map/2
(stdlib) maps.erl:257: :maps.fold_1/3
(elixir) lib/enum.ex:1915: Enum.map/2
(drab) lib/drab/live.ex:894: Drab.Live.assigns_for_partial/5
(drab) lib/drab/live.ex:584: Drab.Live.do_poke/5
(drab) lib/drab.ex:323: anonymous fn/7 in Drab.handle_event/6
You are relying on an implementation detail of structs, can’t you use proper syntax support instead:
iex(1)> defmodule S do
...(1)> defstruct a: 0
...(1)> end
iex(2)> %_{} = %S{}
%S{a: 0}
iex(3)> %_{} = %{}
** (MatchError) no match of right hand side value: %{}