My user object has a boolean property that I want to check as a user logs in.
This is what I'm trying to do, but I am getting a 500 error:
user = User.find_by_email(params['email'])
if user.is_mentor
#do something
end
You need to check to see if there was actually a user that was found with the email params['email']
:
user = User.find_by_email params['email']
if user.present? && user.is_mentor?
# do something
end
Here, user.present?
checks to see if user
is not equal to nil, which is what it would be if no user was found.
Also, the ?
at the end of a method call indicates that the method is returning a boolean value. You should include the question mark in the method call as well if the method is user defined:
def is_mentor?
# do something
end
您需要添加检查以查看是否找到了用户
if user.is_a?(User) && user.is_mentor
You just have to check that the user exists first:
if user && user.is_mentor?
# do something
end
user will be nil and false if Rails didn't find the user.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.