Loop & output content_tags within content_tag in helper

block, helper, ruby-on-rails, ruby-on-rails-3

Solution

Try this:

def foo_list items
  content_tag :ul do
      items.collect {|item| concat(content_tag(:li, item))}
  end
end

Problem

I'm trying a helper method that will output a list of items, to be called like so: ``` foo_list( ['item_one', link_to( 'item_two', '#' ) ... ] ) ``` I have written the helper like so after reading Using helpers in rails 3 to output html: ``` def foo_list items content_tag :ul do items.collect {|item| content_tag(:li, item)} end end ``` However I just get an empty UL in that case, if I do this as a test: ``` def foo_list items content_tag :ul do content_tag(:li, 'foo') end end ``` I get the UL & LI as expected. I've tried swapping it around a bit doing: ``` def foo_list items contents = items.map {|item| content_tag(:li, item)} content_tag( :ul, contents ) end ``` In that case I get the whole list but the LI tags are html escaped (even though the strings are HTML safe). Doing `content_tag(:ul, contents.join("\n").html_safe )` works but it feels wrong to me and I feel `content_tag` should work in block mode with a collection somehow.

Original source

Related problems