简体   繁体   中英

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.

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.

You can also do like this.

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

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM