2017-02-22 88 views
0

我想在Rails中使用助手方法添加下拉菜單,但是當我在下拉鍊接中有ul鏈接時,它會繞過我的下拉鍊接標記。 。繞過鏈接的助手方法的引導下拉菜單

application_helper.rb

def nav_bar(c='nav nav-pills') 
    content_tag(:ul, class: "#{c}") do 
     yield 
    end 
    end 

    def nav_dropdown(text) 
    html_options = {data: {toggle:"dropdown"}, class:"dropdown-toggle", role:"button", aria:{haspopup:"true", expanded:"false"}} 
    content_tag(:li, role:"presentation", class:"dropdown") do 
     link_to raw(text + content_tag(:span, "",class:"caret") ),"#", html_options 
     nav_bar('dropdown-menu') do yield end 

    end 
    end 

_menu.html.erb

時,我有導航欄線 nav_bar('dropdown-menu') do yield end

產生
<%= nav_bar do %> 
    ... 

<% if current_user.admin? %> 
    <%= nav_link t('menu.list_companies'), companies_path %> 
    <%= nav_dropdown t('menu.configurations') do %> 
    <%= nav_link t('menu.edit_process_types'), process_types_path %> 
    <% end %> 

<% end %> 

HTML當我從helper方法去除導航欄線nav_bar('dropdown-menu') do yield end產生

<li role="presentation" class="dropdown"> 
     <ul class="dropdown-menu"> 
     <li><a data-method="get" href="/process_types"><span class="translation_missing" title="translation missing: pt.menu.edit_process_types"> Edit Process Types</span></a> 
     </li> 
     </ul> 
    </li> 

HTML

<li role="presentation" class="dropdown"> 
    <a data-toggle="dropdown" class="dropdown-toggle" role="button" aria-haspopup="true" aria-expanded="false" href="#"> 
    Configurações<span class="caret"></span> 
    </a> 
</li> 

我想達到:

<li role="presentation" class="dropdown"> 
    <a data-toggle="dropdown" class="dropdown-toggle" role="button" aria-haspopup="true" aria-expanded="false" href="#"> 
    Configurações<span class="caret"></span> 
    </a> 
    <ul class="dropdown-menu"> 
    <li><a data-method="get" href="/process_types"><span class="translation_missing" title="translation missing: pt.menu.edit_process_types"> Edit Process Types</span></a> 
    </li> 
    </ul> 
</li> 

任何想法,對什麼是這裏發生的事情如何達到預期的效果?

回答

0

注意到content_tag只是輸出最後一行。 然後我發現這個:rendering text and html in a block of content_tag,在那裏我看到了一個可能的方法來做到這一點。

最後,我改變application_helper.rb與

def nav_dropdown(text) 

    html_options = {data: {toggle:"dropdown"}, class:"dropdown-toggle", role:"button", aria:{haspopup:"true", expanded:"false"}} 
    content_tag(:li, role:"presentation", class:"dropdown") do 

    link= link_to(raw(text + content_tag(:span, "",class:"caret") ),"#", html_options) 

    nav= nav_bar("dropdown-menu") do 
     yield 
     end 

     link + nav 
    end 

    end 

,並最終獲得工作所需的html:

<li role="presentation" class="dropdown"> 
    <a data-toggle="dropdown" class="dropdown-toggle" role="button" aria-haspopup="true" aria-expanded="false" href="#"> 
    Configurações <span class="caret"></span> 
    </a> 
    <ul class="dropdown-menu"> 
      <li class="active"><a data-method="get" href="/process_types">Processos Modelo</a> 
      </li> 
    </ul> 
</li>