2012-11-17 74 views
0

我在創建導航鏈接的應用程序中創建了一個助手。 現在我想爲此鏈接添加一個插入符號,以便我可以在最後得到一個不錯的箭頭。Rails嵌套html幫手

我的HTML應該是這樣的:

<li class='dropdown'> 
    <a class='dropdown-toggle' data-toggle='dropdown' href='#' 
     Dropdown 
     <b class='caret'></b> 
    </a> 

現在,我得到了我的助手的設置是這樣的:

content_tag(:li, class: 'active dropdown') do 
    link_to(text, link, class: 'dropdown-toggle') do 
     content_tag(:b, class: 'caret') 
    end 
    end 

但是當我這樣做,我得到這個錯誤信息:

undefined method `stringify_keys' for "/":String 

我也想添加一些項目到我的下拉,所以我需要嵌套一些,但我不知道如何。有沒有人可以幫助我並指引我朝着正確的方向發展?

謝謝!

回答

3

您正在向link_to傳遞一個塊,因此您不應該將鏈接文本傳遞給它,如in the docs所示。試試這個:

content_tag(:li, class: 'active dropdown') do 
    link_to(link, class: 'dropdown-toggle') do 
    "#{text}#{content_tag(:b, "", class: 'caret')}".html_safe 
    end 
end 
+0

幾乎在那裏,但現在它轉義爲脫字符的html,我已經嘗試了與html_safe的一些組合,但沒有正確的結果。還有其他建議嗎? – Niels

+0

哦,是的。如果沒有塊,content_tag需要標籤內容作爲其第二個參數,所以我添加了一個空字符串。順便說一下,你有沒有考慮過使用':after' CSS選擇器來附加箭頭? –