"if" clause in the view - - - (for two objects)


#1

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


#2

<…>

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


#3

to all the simplistic and obviously wonderful answers (and to those who
know them) , thanks.

Rimantas - thanks again.

smr