views:

127

answers:

3

beside the fact that accessibility standards discourage the use of a link pointing to the current page, how I am supposed to refactor the following view code?

#navigation
  %ul.tabbed
    - if current_page?(new_profile_path)
      %li{:class => "current_page_item"}
        = link_to t("new_profile"), new_profile_path
    - else
      %li
        = link_to t("new_profile"), new_profile_path

    - if current_page?(profiles_path)
      %li{:class => "current_page_item"}
        = link_to t("profiles"), profiles_path
    - else
      %li
        = link_to t("profiles"), profiles_path
    ...

Thank you.

A: 

Looks like a good case for a partial to me.

Chuck
People, comment if you're going to downvote. Otherwise you're not adding to the discussion — you're just being a prick.
Chuck
actually the code already lays in a partial, thank you
Partials could definitely help here, but you were pretty vague. A suggestion as to exactly what would go into the partial would help.
James A. Rosen
+2  A: 
#navigation
  %ul.tabbed
    %li{:class => current_page?(new_profile_path) ? "current_page_item" :nil }
      = link_to t("new_profile"), new_profile_path
    %li{:class => current_page?(profiles_path) ? "current_page_item" :nil }
      = link_to t("profiles"), profiles_path
    ...
Lichtamberg
thank you! Ternary opeator is more concise but I would love to obtain a function for that.
+4  A: 
# helpers
def current_page_class(page)
  return :class => "current_page_item" if current_page?(page)
  return {}
end

-# Haml
#navigation
  %ul.tabbed
    %li{current_page_class(new_profile_path)}
      = link_to t("new_profile"), new_profile_path
    %li{current_page_class(profiles_path)}
      = link_to t("profiles"), profiles_path
    ...
nex3
Excellent! Thank you! P.S.: I think I can discard the last return in the function, isn't it?
That's true; I added it for symmetry with the other `return` and to emphasize that the hash was there as a return value.
nex3