Take me home

HTML list helper

Written by August Lilleaas, published November 21, 2007

Tidy up HTML-list creation in views with one-liner neatness.

I use this one in pretty much all of them projects of mine.

def list(stuff)
  content_tag(:ul, stuff.map { |thingie| content_tag(:li, yield(thingie)) })
end

Usage:

<%= list(@posts) {|post| link_to(post.title, post_url(post)) } %>

Pretty pretty. Much better than this:

<ul>
  <% @posts.each do |post| %>
    <li><%= link_to(...) %></li>
  <% end %>
</ul>

Questions or comments?

Feel free to contact me on Twitter, @augustl, or e-mail me at august@augustl.com.