haml视图中的重构条件

时间:2009-09-02 13:59:43

标签: ruby-on-rails ruby haml

除了可访问性标准阻止使用之外 指向当前页面的链接,我应该如何 重构以下视图代码?

#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
    ...

谢谢。

3 个答案:

答案 0 :(得分:8)

# 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
    ...

答案 1 :(得分:2)

#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
    ...

答案 2 :(得分:0)

看起来对我不利的好例子。