Forum: Ruby on Rails Help with link_to and its kin

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.
78ee9d5c27eebd474b9552c6203571c0?d=identicon&s=25 darklingza@gmail.com (Guest)
on 2008-12-17 20:18
(Received via mailing list)
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
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2008-12-17 20:21
(Received via mailing list)
On 17 Dec 2008, at 17:58, darklingza@gmail.com wrote:

> 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
78ee9d5c27eebd474b9552c6203571c0?d=identicon&s=25 Marc (Guest)
on 2008-12-17 21:32
(Received via mailing list)
On Dec 17, 9:20 pm, Frederick Cheung <frederick.che...@gmail.com>
wrote:
> > to apply class="current" to the tab for the current page.
>    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
This topic is locked and can not be replied to.