I'd use
<% if current_user.try(:organizer?) || current_user.try(:admin?) %>
if you do not need something like this a lot. If you often need permission checks you might want to look into a tool that supports you with this. cancancan
or pundit
comes to mind.
If you want to avoid those ored
checks, and don't want to add pundit or thelikes to your stack, you could add another method to your user model:
def admin_or_operator?
admin? || operator?
end
and then just use that. The feasibility of this approach of course depends on the number of different roles and combinations you need to check.
Also: if you often need to check for the presence of a user (the reason you actually need try
) then you could consider using the nil object pattern.
def current_user
find_actual_user || Guest.new
end
Where Guest
would be an object that responds false
to all those permission checks.
Also some guidelines:
If you want to know more about the reasons for above recommendations you can check stackoverflow or google.