How to build a list containing sublists using content_tag
f% of Rails?
Using the method in Helper does not work.
<ul class="menu">
<li>Item1
<ul>
<li><a href="#>Link1</a></li>
<li><a href="#>Link2</a></li>
<li><a href="#">#
How to build a list containing sublists using content_tag
f% of Rails?
Using the method in Helper does not work.
<ul class="menu">
<li>Item1
<ul>
<li><a href="#>Link1</a></li>
<li><a href="#>Link2</a></li>
<li><a href="#">#
With this code you can create the list:
def li_with_link(link_text, link_url)
content_tag :li, link_to(link_text, link_url)
end
def ul_with_lis
content_tag :ul, class: 'menu' do
content_tag :li do
"Item1" + li_with_link("link1", '#') + li_with_link("link2", '#')
end
end
end
Note: If your idea is to simplify, better use ERB!
Take a look at the method documentation as well: link
This Rodrigo tip has a problem: each link will have a UL + LI, but I agree with it that using ERB (or HAML, or SLIM) is better. If for some reason you need to do this with ruby, maybe this will help:
def menu(links = [])
content_tag :ul do
links.map do |link|
content_tag :li do
link_to link[:label], link[:href]
end
end
end
end
call:
<% links = [] %>
<% links << { label: 'google', href: 'https://google.com' } %>
<% links << { label: 'facebook', href: 'https://facebook.com' } %>
<% links << { label: 'twitter', href: 'https://twitter.com' } %>
<% links << { label: 'logout', href: logout_path %>
<%= menu(links) %>