Jake
Jake

Reputation: 1380

Ruby if not/unless when dealing with multiples

I'm trying to avoid applying a div with link_to on certain webpages. When either of these two out of 20 are present I do NOT want to apply a Skip Link. So I'm leaning towards for more of if not.

So right now I have:

<% if request.original_url != "www.alpha.com" || "www.beta.com"
 <div class="skip">
  <%=link_to('Skipping', '#content')%>
 </div>
<% end %>

This actually doesn't work. It's still applying the div. However when I do it singularly it works. There has to be a better/rails way to conditionally apply when NOT two.

Upvotes: 0

Views: 23

Answers (2)

7urkm3n
7urkm3n

Reputation: 6321

You can also do like this.

unless ["www.alpha.com", "www.beta.com"].include?(request.original_url)
    ...
end

Upvotes: 0

Dave Newton
Dave Newton

Reputation: 160321

That's not how logical expressions work.

url = request.original_url
if (url != "www.alpha.com") && (url != "www.beta.com")
  # ...

More concisely, you can check if the URL is not present in an array of URLs.

Your text implies the opposite of your code; you said you wanted to add the link if the URL does match the two URLs, so you'll also want to figure out what you mean.

Upvotes: 1

Related Questions