def secondary_menu_link_to(link_name, link_path)
if (link_path is the current @controller.controller_path and @controller.action_name)
link_to “&racuo; #{link_name}”, link_path
else
link_to link_name, link_path
end
end
No i teraz pytanie, jak napisać ten warunek “if” w tej metodzie żeby to zadziałało. Nie uzywam niestety przy tworzeniu linków :controller=>…, :action=>… tylko RESTFul resources helperów.
Idealne byłoby rozwiązanie podane w komentarzach, ale chyba jeszcze nikt tego nie wykonał:
highlight_current_link do
link_to 'Home', root_path
link_to 'About', about_path
link_to 'Contact', contact_path
end
Jeśli nie masz zbyt dużo elementów w menu, to możesz skorzystać z innego rozwiązania (również podanego w komentarzach w tym poście). Może nie jest DRY, ale nikt Ci za to głowy nie urwie
def link_to_with_highlight(name, options = {}, html_options = {}) # same sig as #link_to
html_options.merge!({ :class => 'active' }) if current_page?(options)
link_to(name, options, html_options)
end
[quote=rav]Idealne byłoby rozwiązanie podane w komentarzach, ale chyba jeszcze nikt tego nie wykonał:
highlight_current_link do
link_to 'Home', root_path
link_to 'About', about_path
link_to 'Contact', contact_path
end[/quote]
Można dosyć łatwo coś takiego napisać:
[code=ruby]module ApplicationHelper
class CurrentPageDecorator
def initialize(helper,options) @helper = helper @html_class = options[:class] || ‘active’
end
def link_to(*args,&blk)
name = args.first
options = args.second || { }
html_options = args.third || { }
html_options[:class] = @html_class if @helper.current_page?(options) @helper.link_to(name,options,html_options,blk)
end
end