Cannot see links visible for admins on admin account
So I was trying to hide some stuff from normal users: Helper:
def admin User.find(:all, :conditions => { :email => ["admin@admin.com"] }) end
View:
<% if @active_user == admin %> <td><%= link_to raw('<i class="icon-pencil icon-white"></i>'), edit_quiz_path(quiz), :class => 'btn btn-info' %></td> <td><%= link_to raw('<i class="icon-trash icon-white"></i>'), quiz, method: :delete, data: { confirm: 'Pewien?' }, :class => 'btn btn-danger' %></td> <% end %>
In the end, I logged on my admin account (with email admin@admin.com) and nothing showed.
Am I using proper function for finding the user in the db?
Answers
You could rewrite the helper method like this, so it would check the user is an admin without doing a query to the DB:
def admin?(user) user.present? && user.email == 'admin@admin.com' end
Even better, create the method admin? in the User class.
I changed @current_user == admin to current_user.admin? and suddenly everything is working. So in the end it was the view that was wrong.