2009-09-02 4 views
5

접근성 표준이 현재 페이지를 가리키는 링크 의 링크를 사용하지 못하도록하는 대신에 다음 코드를 리팩토링하는 방법을 생각해보십시오.haml 내의 refactor 조건보기

#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

+0

사실입니다. 다른'return'과 대칭을 위해 그것을 추가했고 해쉬가 반환 값으로 존재했음을 강조했습니다. –

0

나에게 부분적으로 좋은 사례 인 것처럼 보입니다.

+0

사람들은 코멘트 당신은 downvote하는 거라면. 그렇지 않으면 토론에 추가하지 않고 있습니다. 단지 멍청이 일뿐입니다. – Chuck

+0

실제로 코드가 이미 부분적으로 놓여 있으며, – user167267

+0

부분적으로 도움이 될 수 있지만 꽤 모호합니다. 부분에 들어가는 것이 정확히 무엇인지에 대한 제안은 도움이 될 것입니다. –

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