简体   繁体   中英

Rails nested html helper

I'm creating a helper in my rails app where I create a navigation link. Now I want to add an caret to this link so I can get a nice arrow at the end.

My html should look like this:

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

Now I got my helpers setup like this:

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

But when I do this I got this error message:

undefined method `stringify_keys' for "/":String

I also want to add some item to my dropdown so I need to nested some more but I don't know how. Is there anybody who could help me and point me in the right direction?

Thanks!

You're passing a block to link_to so you shouldn't pass it link text, as shown in the docs . Try this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM