darklingza-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2008-Dec-17 17:58 UTC
Help with link_to and its kin
Hi Everyone I am a new user of Ruby and Rails, and would like some help with the following problem. I have created a navbar with a styled unordered list and I would like to apply class="current" to the tab for the current page. Currently I am using a large, cumbersome and ugly if..then block, rendering the class="current" into the html and link_tos with current_page?. I am absolutely sure this is a fairly common task for many rails developers, so common and easy in fact, that noone even bothers mentioning it on the web (a quick reference to the fact that I have unsuccessfully googled already). Help will be rewarded with chunky bacon. P&P Not included Thanks Marc --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 17 Dec 2008, at 17:58, darklingza-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote:> > Hi Everyone > > I am a new user of Ruby and Rails, and would like some help with the > following problem. > > I have created a navbar with a styled unordered list and I would like > to apply class="current" to the tab for the current page. > > Currently I am using a large, cumbersome and ugly if..then block, > rendering the class="current" into the html and link_tos with > current_page?. >I would write a small helper, something like def magic_link_to(name, options={}, html_options={}) if current_page?(options) html_options = html_options.merge(:class => ''current'') end link_to name, options, html_options end Then just use magic_link_to instead of link_to Fred> I am absolutely sure this is a fairly common task for many rails > developers, so common and easy in fact, that noone even bothers > mentioning it on the web (a quick reference to the fact that I have > unsuccessfully googled already). > > Help will be rewarded with chunky bacon. > P&P Not included > > Thanks > Marc > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Dec 17, 9:20 pm, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 17 Dec 2008, at 17:58, darklin...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote: > > > > > Hi Everyone > > > I am a new user of Ruby and Rails, and would like some help with the > > following problem. > > > I have created a navbar with a styled unordered list and I would like > > to apply class="current" to the tab for the current page. > > > Currently I am using a large, cumbersome and ugly if..then block, > > rendering the class="current" into the html and link_tos with > > current_page?. > > I would write a small helper, something like > > def magic_link_to(name, options={}, html_options={}) > if current_page?(options) > html_options = html_options.merge(:class => ''current'') > end > link_to name, options, html_options > end > > Then just use magic_link_to instead of link_to > > Fred >Thanks for that Fred Added the magic and its working embarrasingly well. I think I was stuck in the cycle of coming up with increasingly complex solutions to a simple problem, perhaps its time for some coffee. Thanks again Marc --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---