suchen

Heim  >  Fragen und Antworten  >  Hauptteil

ruby - content_for的li>ul>li类型的嵌套

在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中文网2771 Tage vor789

Antworte allen(1)Ich werde antworten

  • 某草草

    某草草2017-04-25 09:03:45

    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

    Antwort
    0
  • StornierenAntwort