ruby - content_for的li>ul>li类型的嵌套
PHP中文网
PHP中文网 2017-04-25 09:01:45
0
1
804

在Rails的helper中,我建立这样一个方法:

def ul_li_ul_nest
  fruit = %w(apple watermelon orange pear pineapple)
  info = %w(price place weight)
  content_tag(:ul) do
    fruit.collect do |i|
       content_tag(:li) do
        i 
        content_tag :ul do
          info.collect do |ii|
            content_tag :li do
              ii
            end
          end.join('').html_safe
        end
      end
    end.join('').html_safe
  end
end

它输出的结果是:

<ul>
  <li>
  <ul>
    <li>price</li>
    <li>place</li>
    <li>weight</li>
  </ul>
  </li>
  <li>
  <ul>
    <li>price</li>
    <li>place</li>
    <li>weight</li>
  </ul>
  </li>
  <li>
  <ul>
    <li>price</li>
    <li>place</li>
    <li>weight</li>
  </ul>
  </li>
  <li>
  <ul>
    <li>price</li>
    <li>place</li>
    <li>weight</li>
  </ul>
  </li>
  <li>
  <ul>
    <li>price</li>
    <li>place</li>
    <li>weight</li>
  </ul>
  </li>
</ul>

而我期望的输出结果是:

<ul>
  <li>
    apple
    <ul>
      <li>price</li>
      <li>place</li>
      <li>weight</li>
    </ul>
  </li>
  <li>
    watermelon
    <ul>
      <li>price</li>
      <li>place</li>
      <li>weight</li>
    </ul>
  </li>
  <li>
    orange
    <ul>
      <li>price</li>
      <li>place</li>
      <li>weight</li>
    </ul>
  </li>
  <li>
    pear
    <ul>
      <li>price</li>
      <li>place</li>
      <li>weight</li>
    </ul>
  </li>
  <li>
  pineapple
    <ul>
      <li>price</li>
      <li>place</li>
      <li>weight</li>
    </ul>
  </li>
</ul>

如何修改才能达到这种结果?

PHP中文网
PHP中文网

认证0级讲师

reply all(1)
某草草
def ul_li_ul_nest
  fruit = %w(apple watermelon orange pear pineapple)
  info = %w(price place weight)
  content_tag(:ul) do
    fruit.collect do |i|
       content_tag(:li) do
        i.html_safe + content_tag(:ul) do
          info.collect do |ii|
            content_tag :li do
              ii
            end
          end.join('').html_safe
        end
      end
    end.join('').html_safe
  end
end
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template