Reputation: 59
I can't figure out how to do the check correctly to hide the actions from the user
<%= if @current_user.id == @post.user_id do %>
<span>
<%= link "Edit", to: Routes.post_path(@conn, :edit, @post) %>
</span>
<span>
<%= link "Delete", to: Routes.post_path(@conn, :delete, @post), method: :delete, data: [confirm: "Are you sure"] %>
</span>
<% end %>
When the user is not logged in, this error appears:
UndefinedFunctionError at GET
/posts/1
functionnil.id/0
is undefined
Upvotes: 1
Views: 252
Reputation: 3134
In this case, your @current_user
is nil
, so Elixir tries to run nil.id
which fails.
You could probably solve your issue by checking for nil
first:
<%= if @current_user && @current_user.id == @post.user_id do %>
The error message is a bit confusing because nil
is also an atom, and the a.b
syntax has two uses in Elixir:
:b
on a map/struct a
(what you are trying to do)a.b()
function, with a
being a module atomnil
also being an atom, it gets interpreted as nil.id()
, but the nil.id/0
function doesn't exist since nil
is not an actual module.
Upvotes: 8