Trying to get "current user" in Rails -


in /views/layouts directory, how current user? using devise, current_user not work here reason, describes unknown method. want like:

<% if user.role? == "gen_admin" %>     <li>     <%= link_to('admin', users ) %>     </li> <% end %> 

i have role? method defined in user model, still exception:

undefined method 'role?' #<class:0x3fcc1e0>

so how can current user, , access fields @ level of source tree? thanks!

here roles? method:

# in user roles = %w[gen_admin teacher_admin student] def role?(base_role)   roles.index(base_role.to_s) <= roles.index(role) end 

your code must account when users logged in, , when not logged in.

if no user logged in, current_user return nil (as in case, thought error on devise's part).

your view code must handle - eg.

<% if current_user.present? && current_user.role?('gen_admin') %> 

Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -