2009-09-02 22 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