I''m throwing this out there to see if anyone can improve this, or will
find it at all useful.
link_to_if_current only gives a non-link if the generated URL matches
the request URI exactly.
IE:
url_for(options) == @request.request_uri
It''s a common theme to do the same for entire sections so that the link
will be marked if you are already in that section.
At the moment, I''ve just quickly knocked up this helper called (rather
verbosely) link_to_and_mark_current_section
def link_to_and_mark_current_section(name, options = {}, html_options =
{}, *parameters_for_method_reference)
new_class = if (html_options[:class].nil?)
''current''
else
html_options[:class] = '' current''
end
html_options[:class]= new_class if (
options[:controller].nil? || controller.controller_name ==
options[:controller]
) && (
options[:action].nil? || options[:action] == params[:action]
)
link_to(name, options, html_options, *parameters_for_method_reference)
end
This will give any link which is to the same controller and action (if
set) a class called ''current''.
It works fine, but obviously isn''t good with anything except
controllers
and actions.
Is there an easy way to rewrite this taking into accounts of what routes
knows, so will work for any URL?
Thanks.
--
R.Livsey
http://livsey.org