Take me home

HTML list helper

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)) })


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

Pretty pretty. Much better than this:

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

Questions or comments?

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