Forum: Ruby on Rails "if" clause in the view - - - (for two objects)

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ce953bec3af375ddc75e375233112b28?d=identicon&s=25 anonymous coward (Guest)
on 2006-05-28 12:45
Hi,
sorry to bother you guys with a simple sytnax question;
i have a loop of objects taking place (ie, for page in
@pages....xxxxxxx....end) and a link associated to each pages so that in
the end it looks like this:

page1 (link)
page2 (link)
page3 (link)
.
.
.
page n (link)

(all of this done by putting a simple 'link to' in the for loop.)

now i need to seperate two pages from the rest and link them to
somewhere else than the rest(let's say page1 and page2 with the
corresponding id's 1, 2 linked to 'different' action with the id as
passed paramateres)...my tries where somewhere along the line of
--------
<%= link_to "regular", { :controller => 'regular' ,:action => 'show',
:id => page}  unless page.id == 1||2 %>
<%= link_to "different", { :controller => 'different',  :action =>
'different', :id => page } if page.id == 1||2 %>
--------
or something similar. . .

but oddly enough when i use ||, it just puts all of the links to
different, instead of only 1 or 2. the weird part (to me) is that when i
take off the" || "
it works fine. (meaning, i can get my goal through if i do it with only
one page(either id=1 or id=2) , but when i try doing it on the both of
them with the same command, my idea doesn't come through as i would have
liked it to...(no error involved, just not doing the magic!)
i know i must be missing something really simple, and so here i am
asking for that simple answer, if anyone would be as kind enough to pass
it along...

thanks for your help!

smr
Ec5a599777854c540fd102ef4691fe10?d=identicon&s=25 Rimantas Liubertas (Guest)
on 2006-05-28 13:15
(Received via mailing list)
<...>
> different, instead of only 1 or 2. the weird part (to me) is that when i
> take off the" || "
> it works fine. (meaning, i can get my goal through if i do it with only
> one page(either id=1 or id=2) , but when i try doing it on the both of
> them with the same command, my idea doesn't come through as i would have
> liked it to...(no error involved, just not doing the magic!)

Let's see how if page.id == 1||2 works. "==" has higher precedence
than "||", so this can
be written as:

if (page.id == 1) || 2). This is the same as if you have written "if
true", because
(page.id == 1) || 2 evaluates to "true" if page.id is 1, and evaluates
to 2 if page.id is not 1.
2 is true in boolean context, so no wonder this condition is always
satisfied.

What you want is:
 ... if page.id == 1 || page.id == 2

or, even better
if [1,2].include? page.id
Ce953bec3af375ddc75e375233112b28?d=identicon&s=25 anonymous coward (Guest)
on 2006-05-28 14:11
to all the simplistic and obviously wonderful answers (and to those who
know them) , thanks.

 Rimantas - thanks again.

smr
This topic is locked and can not be replied to.