2009-09-02 10 views
5

के भीतर रिफैक्टर की स्थिति इस तथ्य के बगल में कि अभिगम्यता मानक वर्तमान पृष्ठ पर इंगित लिंक के उपयोग को हतोत्साहित करते हैं, मुझे को निम्नलिखित दृश्य कोड को दोबारा कैसे माना जाता है?हैमल व्यू

#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

बढ़िया! धन्यवाद! पीएस .: मुझे लगता है कि मैं समारोह में आखिरी वापसी को त्याग सकता हूं, है ना? – user167267

+0

यह सच है; मैंने इसे अन्य 'रिटर्न' के साथ समरूपता के लिए जोड़ा और जोर दिया कि हैश रिटर्न वैल्यू के रूप में था। –

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

संबंधित मुद्दे