Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails content_tag for <ul> element

How would I get the following html output with the content_tag helper?

  <ul>
    <li>1</li>
    <li>2</li>
    <li>3</li>
    <li>4</li>
    <li>5</li>
  </ul>

This is what I have currently

  content_tag(:ul) do
    a=*(1..5)
    a.each do |step_number|
      content_tag(:li, class: "step") do
        puts step_number
      end
    end
  end

Update - Answer

Thanks to Peter's Loop & output content_tags within content_tag in helper link below, I was missing concat on the li items

  content_tag :ul do
    items.collect do |step_number|
      concat(content_tag(:li, step_number, class: "step"))
    end
  end
like image 280
Elijah Murray Avatar asked Nov 01 '22 09:11

Elijah Murray


1 Answers

I guess all you have to fix is

content_tag(:ul) do
  (1..5).to_a.map do
    content_tag(:li, step_number, class: "step)
  end.reduce(&:+)
end

That should do the job!

like image 174
Danny Avatar answered Nov 15 '22 04:11

Danny