Hello I am new to rails and having trouble with the following: What is wrong with this code from one of my layout pages? Only the third link_to is displayed on the page. The first two are apparently ignored. <% if condition == true link_to ''Index'', :action => ''index'' link_to ''Summary'', :action => ''summary'' link_to ''Admin'', :action => ''admin'' end -%> Thanks for your help Dewey BTW, Is there a better place to post this type of newbie question?
Hi Dewey, On 22.8.2005, at 10.07, Dewey Howell wrote:> > <%> if condition == true > link_to ''Index'', :action => ''index'' > link_to ''Summary'', :action => ''summary'' > link_to ''Admin'', :action => ''admin'' > end > -%>The <%= tags work so that the whole block is evaluated and the result is then substituted in the template. The block can only return one value. So in your case, only the last row is considered as the "return value" (just like in ruby functions). Therefore, you should have a separate <%= block for every separate value you want to put on the page: <% if condition %> <%= link_to ''Index'', :action => ''index'' %> <%= link_to ''Summary'', :action => ''summary'' %> <%= link_to ''Admin'', :action => ''admin'' %> <% end %> (Another tip: you don''t need the ''== true'' part in the if clause if the condition is a boolean value. I hence left it out from my example)> > Thanks for your help > Dewey > > BTW, Is there a better place to post this type of newbie question?This mailing list is a fine place for them. //jarkko -- Jarkko Laine http://jlaine.net http://odesign.fi _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Dewey Howell wrote:>Hello >I am new to rails and having trouble with the following: > >What is wrong with this code from one of my layout pages? Only the >third link_to is displayed on the page. The first two are apparently >ignored. > ><%> if condition == true > link_to ''Index'', :action => ''index'' > link_to ''Summary'', :action => ''summary'' > link_to ''Admin'', :action => ''admin'' > end >-%> > >Thanks for your help >Dewey > >BTW, Is there a better place to post this type of newbie question? >_______________________________________________ >Rails mailing list >Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >http://lists.rubyonrails.org/mailman/listinfo/rails > > >You can also construct an array of strings to return, and Rails will output each one. Like this: <% retval = [] if condition retval.push(link_to ''Index'', :action => ''index'') retval.push(link_to ''Summary'', :action => ''summary'') retval.push(link_to ''Admin'', :action => ''admin'') end %> Cheers, Jeremy -- Jeremy Stephens Computer Systems Analyst I School of Medicine Department of Biostatistics Vanderbilt University
Jeremy Stephens wrote:> You can also construct an array of strings to return, and Rails will > output each one.If you really wanted to something so obtuse, you might as well do it this way: <%= [link_to(''Index'', :action => ''index''), link_to(''Summary'', :action => ''summary''), link_to(''Admin'', :action => ''admin'')] if condition %> But the best answer has been provided by Jarkko.
Nicholas Seckar wrote:> Jeremy Stephens wrote: > >> You can also construct an array of strings to return, and Rails will >> output each one. > > > If you really wanted to something so obtuse, you might as well do it > this way: > > <%= [link_to(''Index'', :action => ''index''), > link_to(''Summary'', :action => ''summary''), > link_to(''Admin'', :action => ''admin'')] if condition %> > > But the best answer has been provided by Jarkko. > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >What''s so obtuse about it? I was only trying to present an alternative solution. No need to be rude. -- Jeremy Stephens Computer Systems Analyst I School of Medicine Department of Biostatistics Vanderbilt University
Or... <% if condition concat link_to(''Index'', :action => ''index''), binding concat link_to(''Summary'', :action => ''summary''), binding concat link_to(''Admin'', :action => ''admin''), binding end %> I personally like using concat the most as it is similar to echo / response.write. Old habits die hard. The [] if condition is slick too though. On 8/22/05, Jeremy Stephens <jeremy.f.stephens-GISIphQPqQyKDjD76uxLDg@public.gmane.org> wrote:> Nicholas Seckar wrote: > > > Jeremy Stephens wrote: > > > >> You can also construct an array of strings to return, and Rails will > >> output each one. > > > > > > If you really wanted to something so obtuse, you might as well do it > > this way: > > > > <%= [link_to(''Index'', :action => ''index''), > > link_to(''Summary'', :action => ''summary''), > > link_to(''Admin'', :action => ''admin'')] if condition %> > > > > But the best answer has been provided by Jarkko. > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > What''s so obtuse about it? I was only trying to present an alternative > solution. No need to be rude. > > -- > Jeremy Stephens Computer Systems Analyst I School of Medicine > Department of Biostatistics Vanderbilt University > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Brock Weaver [OBC]Technique
The problem is that you need to wrap all of your link_to''s in a separate ERB tag (ie a <%= %> tag) as an ERB tag will only display the last evaluated result. That''s why it''s only displaying your last link_to. If you really wanted it all to be in the one ERB tag, just assign the results of each to a temp string, and put that on its own line at the end. Julian. On 22/08/2005, at 5:07 PM, Dewey Howell wrote:> Hello > I am new to rails and having trouble with the following: > > What is wrong with this code from one of my layout pages? Only the > third link_to is displayed on the page. The first two are apparently > ignored. > > <%> if condition == true > link_to ''Index'', :action => ''index'' > link_to ''Summary'', :action => ''summary'' > link_to ''Admin'', :action => ''admin'' > end > -%> > > Thanks for your help > Dewey > > BTW, Is there a better place to post this type of newbie question? > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Hi ! Julian Leviston said the following on 2005-08-22 18:57:> The problem is that you need to wrap all of your link_to''s in a > separate ERB tag (ie a <%= %> tag) as an ERB tag will only display the > last evaluated result. That''s why it''s only displaying your last > link_to. If you really wanted it all to be in the one ERB tag, just > assign the results of each to a temp string, and put that on its own > line at the end.No need to be that complicated: <% if condition == true link_to ''Index'', :action => ''index'' + link_to ''Summary'', :action => ''summary'' + link_to ''Admin'', :action => ''admin'' end -%> Notice the concatenation operator at the end of the first two links. Bye ! François
On Aug 23, 2005, at 9:00 AM, François Beausoleil wrote:> Hi ! > > Julian Leviston said the following on 2005-08-22 18:57: > >> The problem is that you need to wrap all of your link_to''s in a >> separate ERB tag (ie a <%= %> tag) as an ERB tag will only >> display the last evaluated result. That''s why it''s only >> displaying your last link_to. If you really wanted it all to be >> in the one ERB tag, just assign the results of each to a temp >> string, and put that on its own line at the end. >> > > No need to be that complicated: > > <%> if condition == true > link_to ''Index'', :action => ''index'' + > link_to ''Summary'', :action => ''summary'' + > link_to ''Admin'', :action => ''admin'' > end > -%> > > Notice the concatenation operator at the end of the first two links.That doesn''t work because of Ruby''s operator precedence rules. Or at least that''s what I''ve deduced, because I was unable to find an operator precedence table for Ruby on the web, and my copy of _The Ruby Way_ doesn''t list parentheses-less method calls'' precedence in its table. Ruby thinks you want to concatenate the string "index" and not the result of the method. The fix: put the parens around each method argument list. Stuff like this happens all the time. You write an expression using Ruby''s groovy, code readability enhancing syntactic shortcuts, only to find that you need to go back and re-write your expressions if you want to compose them in certain ways. Smalltalk, which Ruby owes a certain debt to, dispenses with operator precedence entirely. So do Lisps, by their very nature. And speaking of Lisp, Ruby''s flexibility lets you write code that has a resemblance to Lisp: <% if true == true (link_to ''Index'', :action => ''index'') + (link_to ''Summary'', :action => ''summary'') + (link_to ''Admin'', :action => ''admin'') end %> Regards, Ed -- Transmogrify, LLC * <http://xmog.com/>