如何使用 content_tag 帮助程序获得以下 html 输出?
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
</ul>
这是我目前拥有的
content_tag(:ul) do
a=*(1..5)
a.each do |step_number|
content_tag(:li, class: "step") do
puts step_number
end
end
end
更新-答案
感谢彼得的 Loop & output content_tags within content_tag in helper下面的链接,我在 li
项上遗漏了 concat
content_tag :ul do
items.collect do |step_number|
concat(content_tag(:li, step_number, class: "step"))
end
end
最佳答案
我猜你只需要解决
content_tag(:ul) do
(1..5).to_a.map do
content_tag(:li, step_number, class: "step)
end.reduce(&:+)
end
这应该可以完成工作!
关于html - <ul> 元素的 Rails content_tag,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20386324/