2017-06-03 23 views
0

說我有一個navbar鏈接導航欄在使用Rails和引導

<ul class="nav navbar-nav"> 
    <li><%= link_to 'Item 1', item1_path %> 
    <li><%= link_to 'Item 2', item2_path %> 
    ... 

不過這樣一來當前菜單項也是一個鏈接。我如何將它變成span,保持外觀?這看起來不是一個好的選擇,因爲在這種情況下,我需要複製bootstrap的樣式。或者讓它看起來像一個沒有指向任何頁面的菜單項?

+0

採取一個例子你使用bootstrap?我認爲bootstrap的方法是爲當前鏈接添加一個「活動」類。 – wesley6j

+0

到當前**鏈接**。這纔是重點。當前菜單項不應該是鏈接。 –

+0

爲什麼你不使用'link_to_unless_current'? – jvillian

回答

-1

我想出了下面的幫助:

module ApplicationHelper 
    def menu_item(name, path) 
    if current_page? path 
     content_tag('a', name) 
    else 
     link_to name, path 
    end 
    end 
end 

隨着我們剛剛得到了原代碼menu_item更換link_to

0

一個的link_to方法將總是渲染<a>標籤,但你可以渲染<a>標籤的<span>如果使用link_to ... do

這是this文檔

<%= link_to(@profile) do %> 
    <strong><%= @profile.name %></strong> -- <span>Check it out!</span> 
<% end %> 
<!-- which renders --> 
<a href="/profiles/1"> 
     <strong>David</strong> -- <span>Check it out!</span> 
</a> 
+0

我不需要'span'。我要麼保持樣式的'span'。在這種情況下,我需要複製'bootstrap'的樣式。因此我認爲這不是一個好的選擇。或者使其他方式使當前菜單項看起來像菜單項,但不指向任何頁面。就像我的回答一樣。無論如何,你最好使用'link_to_unless_current'。 –

+0

@ x-yuri您還可以設計您創建的新跨度元素,以創建第一個效果 –

+0

這將使我複製Bootstrap樣式。我在前面的評論中談到了這一點。我想避免這種情況。 –