So in my rails app, I have users with different permissions. When someone is logged in, everything works perfectly. However, when no one is logged in, I get the following error:
Undefined method 'editor?' for nil:NilClass
def require_editor
redirect_to '/error' unless current_user.editor? || current_user.admin?
end
I think I understand exactly why this is happening: because no one is logged in, Rails is trying to figure out what role they have, and because there is no role for someone not logged it, Rails doesn't know if it should show the page or not.
My question is: How can I make a default role for someone who is not logged in, and send them to the error page that says they don't have permission?
current_useris justnilandnildoesn't respond toeditor?oradmin?.nilis Ruby's way to describe "nothing". But "someone not logged it" is not nothing. That nothing is something (excellent talk BTW). Using an object in place of missing one is known as the Null Object pattern and it's an elegant way to approach this problem. Where doescurrent_usercome from? Are you using devise?