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:

  &lt;% @posts.each do |post| %&gt;
    &lt;li&gt;&lt;%= link_to(...) %&gt;&lt;/li&gt;
  &lt;% end %&gt;

Questions or comments?

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