anonymous coward
2006-May-28 10:46 UTC
[Rails] "if" clause in the view - - - (for two objects)
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 -- Posted via http://www.ruby-forum.com/.
Rimantas Liubertas
2006-May-28 11:13 UTC
[Rails] "if" clause in the view - - - (for two objects)
<...>> ...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!)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 -- Regards, Rimantas -- http://rimantas.com/
Apparently Analagous Threads
- [PATCH v2] Btrfs: improve multi-thread buffer read
- [PATCH RFC] Btrfs: improve multi-thread buffer read
- How to get rid of page 1 and 4 i.e Only dots pages while generating the graphs?
- Site Navigation With Polymorphic Has Many Through
- [patch V3 25/37] mm/highmem: Provide kmap_local*