2009-09-02 82 views
5

旁边的事实,可访问性标准阻止指向当前页面的链接使用 ,我应该如何 重构以下视图代码?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 
    ... 

谢谢。

回答

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 
    ... 
+0

优秀!谢谢! P.S .:我想我可以放弃最后的回报,不是吗? – user167267 2009-09-03 16:20:05

+0

这是真的;我添加了它与另一个'return'对称,并强调hash是作为返回值存在的。 – 2009-09-15 09:56:42

0

看起来像是对我偏爱的好例子。

+0

人,评论,如果你要downvote。否则,你不会加入讨论 - 你只是一个棘手的问题。 – Chuck 2009-09-02 17:31:53

+0

实际上代码已经在部分中,谢谢 – user167267 2009-09-03 16:29:13

+0

部分可以在这里帮助,但你很模糊。关于如何进入部分的建议将有所帮助。 – 2009-09-16 18:17:14

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 
    ... 
+0

谢谢!三元操作符更简洁,但我很想获得一个功能。 – user167267 2009-09-03 16:18:47