Leo Costa Leo Costa - 2 months ago 12
HTML Question

Link to Admin Dashboard in View

I already have Active Admin all set up and now I was trying to add a link to the dashboard in my view.

I have the current_admin_user method in my application_controller:

def current_admin_user
return nil if user_signed_in? && !current_user.admin?
current_user
end


And my view is:

<% if current_admin_user %>
<li><%= link_to "Admin", admin_path %></li>
<% end %>


However I'm getting the error:

undefined local variable or method `current_admin_user'


Anyone knows how to solve this?

Answer

Put the function in your application_helpers.rb file in the /helpers folder instead and it should work

  def current_admin_user
    return nil if user_signed_in? && !current_user.admin?
    current_user
  end

An alternative is to use if user_signed_in? && current_user.admin? in your view

  <% if user_signed_in? && current_user.admin? %>
    <li><%= link_to "Admin", admins_path %></li>
  <% end %>
Comments