Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make @current_user available in form.html.eex?

Tags:

I'd like to add logic into web/templates/user/form.html.eex which gets rendered in user#new and user#edit depending on @current_user. But if I do the following code I get an assign @current_user not available in eex template error.

<%= if @current_user do %>
  <h1>Example</h1>
<% end %>

How can I make @current_user available in the form.html.eex?

like image 600
wintermeyer Avatar asked May 03 '16 08:05

wintermeyer


1 Answers

Assuming you are rendering form.html.eex using something like:

render("form.html.eex", changeset: @changeset, conn: @conn)

And you are rendering the "parent" template with @current_user available in assigns.

You can pass @current_user in the render function in your template:

render("form.html.eex", changeset: @changeset, conn: @conn, current_user: @current_user)

You can pass everything in assigns with:

render("form.html.eex", @conn.assigns)

However you should do this with caution, remember explicit is better than implicit.

like image 118
Gazler Avatar answered Sep 28 '22 01:09

Gazler