कई उत्तर पहले से ही हैं, लेकिन यहाँ मैंने बूटस्ट्रैप आइकन को सक्रिय लिंक के साथ काम करने के लिए लिखा है। आशा है कि यह किसी की मदद करेगा
यह सहायक आपको देगा:
- कस्टम तत्व युक्त लिंक के साथ एलई तत्व
- वैकल्पिक बूटस्ट्रैप आइकन
- जब आप दाहिने पृष्ठ पर होंगे तो सक्रिय हो जाएंगे
इसे अपने application_helper.rb में डालें
def nav_link(link_text, link_path, icon='')
class_name = current_page?(link_path) ? 'active' : ''
icon_class = "glyphicon glyphicon-" + icon
content_tag(:li, :class => class_name) do
(class_name == '') ? (link_to content_tag(:span, " "+link_text, class: icon_class), link_path)
: (link_to content_tag(:span, " "+link_text, class: icon_class), '#')
end
end
और लिंक का उपयोग करें:
<%= nav_link 'Home', root_path, 'home' %>
अंतिम तर्क वैकल्पिक है - यह लिंक पर आइकन जोड़ देगा। ग्लिफ़ आइकन के नाम का उपयोग करें। यदि आप बिना टेक्स्ट के आइकन चाहते हैं:
<%= nav_link '', root_path, 'home' %>