2016-09-23 52 views
0

我需要爲子菜單下劃線活動菜單項。我發現我可以在我的菜單中使用以下內容。我能做到這一點通過應用類:Rails - 不適用於鏈接到控制器/動作的類

<li><%= link_to "* RFQ", rfq_path(1), class: get_rfqs_class() %></li>

,並有

def get_rfqs_class 
    if controller_name == 'rfqs' 
     return 'active' 
    end 
    end 

一個輔助項目我有一個get_xxx_class()爲每個項目和相應的幫手。

這很好。不過,我需要爲另一個菜單項

<li> <%= link_to "Pre Production Meeting", controller: 
'pre_production_meetings', action: 'show', id: 1 , 
op_id: $current_opportunity, class: get_pre_production_meetings_class() %> </li> 

的問題是,不同的link_to格式的結果在下劃線不顯示做一點點不同的link_to。如果我做了檢查,但它確實顯示類爲主動,但我沒有看到下劃線

<a href="/pre_production_meetings/1?class=active">Pre Production Meeting</a> 

我嘗試使用的div並用下面的代碼跨越,但沒有運氣。

<% if controller_name == 'pre_production_meetings' then %> 
       <li> <%= link_to "Pre Production Meeting", controller: 'pre_production_meetings', action: 'show', id: 1 , op_id: $current_opportunity, class: get_pre_production_meetings_class() %> </li> 
      <% else %> 
       <li> <%= link_to "Pre Production Meeting", controller: 'pre_production_meetings', action: 'show', id: 1 , op_id: $current_opportunity %></li> 

      <% end %> 

有沒有什麼地方使用link_to控制器/行動我做的方式將阻止類被正確應用?

另外,我檢查的菜單項工作作爲預期

ul.art-hmenu>li>a.active { 
    padding: 0 12px; 
    margin: 0 auto; 
    color: #145366; 
    text-decoration: underline; 
} 

如果我檢查預生產會議風格的樣式,我沒有看到a.active但見a.hover

ul.art-hmenu>li>a:hover, .desktop ul.art-hmenu>li:hover>a { 
    color: #000000; 
    text-decoration: none; 
} 

如果我將文本修飾更改爲下劃線,則會看到下劃線如預期的那樣。

回答

1

那的link_to的情況下使用這種格式:

link_to(body, url_options = {}, html_options = {}) 

你需要圍繞在大括號您的網址選項,以便鏈接看起來是這樣的:

<a href="/pre_production_meetings/1" class="active"> 

你的類被內嵌到url而不是html。

這應該解決您的問題:

<%= link_to "Pre Production Meeting", 
      {controller: 'pre_production_meetings', action: 'show', id: 1 , 
       op_id: @current_opportunity}, 
      class: get_pre_production_meetings_class() %> 

此外,$可能是一個錯字。我假設它是@。

+0

神奇 - 它現在完美。 –

相關問題