Rails:无法嵌套content_tag

时间:2013-02-20 23:38:39

标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-3.2

我编写了一个Rails助手来创建一个嵌套的HTML列表。但是,这不能正确呈现。我做错了什么?

辅助

content_tag :ul do
  [1, 2].each do |x|
    content_tag(:li, x)
    content_tag :ul do
      ['a', 'b'].each do |y|
        content_tag(:li, y)
        content_tag :ul do
          ['i', 'ii'].each do |z|
            content_tag(:li, z)
          end
        end
      end
    end
  end
end

预期输出

<ul>
  <li>1</li>
  <ul>
    <li>a</li>
    <ul>
      <li>i</li>
      <li>ii</li>
    </ul>
    <li>b</li>
    <ul>
      <li>i</li>
      <li>ii</li>
    </ul>
  <li>2</li>
  <ul>
    <li>a</li>
    <ul>
      <li>i</li>
      <li>ii</li>
    </ul>
    <li>b</li>
    <ul>
      <li>i</li>
      <li>ii</li>
    </ul>
  </ul>
</ul>

实际输出

<ul></ul>

1 个答案:

答案 0 :(得分:7)

这是解决方案:

content_tag(:ul) do
  [1, 2].map do |x|
    content_tag(:li, x) <<
    content_tag(:ul) do
      ['a', 'b'].map do |y|
        content_tag(:li, y) <<
        content_tag(:ul) do
          ['i', 'ii'].map do |z|
            content_tag(:li, z)
          end.join('').html_safe
        end
      end.join('').html_safe
    end
  end.join('').html_safe
end

问题是您必须在每个content_tag块的末尾返回一个HTML安全字符串:

# returns <li>asd</li>
content_tag('li') { 'asd' }

# returns <ul><li>asd</li></ul>
content_tag('ul') { content_tag('li') { 'asd' } }

# returns [1, 2]
[1, 2].each{ |x| content_tag('li', x) }

# returns ["<li>1</li>", "<li>2</li>"]
[1, 2].map{ |x| content_tag('li', x) }

# returns "<li>1</li><li>2</li>" not marked as HTML safe, 
# because `join` generates a new string
[1, 2].map{ |x| content_tag('li', x) }.join('')

# returns "<li>1</li><li>2</li>" marked as HTML safe
[1, 2].map{ |x| content_tag('li', x) }.join('').html_safe

# returns "<ul><li>1</li><li>2</li></ul>"
content_tag('ul') do
  [1, 2].map{ |x| content_tag('li', x) }.join('').html_safe
end

# returns "<ul><li>1</li><li>2</li></ul>"
content_tag('ul') do
  content_tag('li') { 'asd' } # this is ignored, since it is not returned
  [1, 2].map{ |x| content_tag('li', x) }.join('').html_safe
end

# returns "<ul><li>asd</li><li>1</li><li>2</li></ul>"
content_tag('ul') do
  content_tag('li') { 'asd' } <<
    [1, 2].map{ |x| content_tag('li', x) }.join('').html_safe
end