Reddirt
Reddirt

Reputation: 5953

RAILS or in if statement

I'm not sure how to use the OR in a view IF statement.

This doesn't work:

<% if current_user.employee.emptype.typename == "ADMIN" or "TECH" %>

Thanks!

Upvotes: 0

Views: 72

Answers (3)

awendt
awendt

Reputation: 13683

That won't work, and it has nothing to do with views. In Ruby, all strings evaluate to true in if statements.

You have to check both values against the type name, like so:

if current_user.employee.emptype.type_name == "ADMIN" || current_user.employee.emptype.type_name == "TECH"

or check if it's contained within an array of valid types:

if ["ADMIN", "TECH"].include?(current_user.employee.emptype.type_name)

Please also see this article for a comparison of || and or in Ruby.

Upvotes: 0

Mike G
Mike G

Reputation: 125

You can do this one of two ways. I prefer the first method.

<% if current_user.employee.emptype.typename.in?(["ADMIN", "TECH"]) %>

...or...

<% if current_user.employee.emptype.typename == "ADMIN" || current_user.employee.emptype.typename == "TECH" %>

Upvotes: 0

pierallard
pierallard

Reputation: 3371

Write something like this :

<% if current_user.employee.emptype.typename == "ADMIN" || current_user.employee.emptype.typename == "TECH" %>

Or better

<% if ['TECH', 'ADMIN'].include?(current_user.employee.emptype.typename) %>

Be careful with OR and AND keywords, they don't have same operators priority than && and ||

Upvotes: 2

Related Questions