0

I've looked at this article and am getting strange behavior in a HAML partial. When I access two different controller actions, one instance works while the other fails. Here's the code:

= link_to_unless_current(t('some.string', :en=>'SomeString'), '/url',{:class=>(controller.controller_name == 'randomController' ? 'current' : 'header-link')})

When I output = controller.controller_name in either view, I get 'randomController.' On the main listing page (where all objects are shown paginated), the class is not applied, but when moving to the 'show' page, the class suddenly appears. As the controller is the same in both (same string is printed in either case), why is it that the class isn't applied equally?

In general, is there a better way to style links based on the current controller, instead of checking the controller name? The current_page helper requires both controller + action, meaning it's not a fitting candidate here.

Community
  • 1
  • 1
clekstro
  • 593
  • 4
  • 10
  • Just saw that you can pass a block to link_to_unless_current that is executed on the current page. Just included a span with the class I was wanting and it works! – clekstro Jan 28 '12 at 23:34

1 Answers1

0

I'm going to mark this as the answer in case others stumble over the same problem. Hopefully this formats properly:

     = link_to_unless_current(t('some.string', :en=>'SomeString'), '/url',{:class=>(controller.controller_name == 'randomController' ? 'current' : 'header-link')})  do 
        %span.current
            = (t('some.string', :en=>'SomeString'

This can be obviously be cleaned up to remove repeated elements, and will probably end up as an application decorator using draper so that I can use it within any view on the site.

clekstro
  • 593
  • 4
  • 10