0

わかりましたので、私はこのループを持っています

def page_children(page)
  content_tag :li do
    link_to page.url, "/#{page.url_path}/#{page.url}"
    if page.children.any?
      content_tag :ul do
        page_children(page)
      end
    end
  end
end

そして、私は得続けますstack level too deep

私はこの宝石をツリー構造に使用しており、モデルとhamlは次のとおりです

class Page < ActiveRecord::Base  
  acts_as_tree :order => 'sort_order'
  belongs_to :parent, :class_name => "Page" 
  scope :parent_nav, lambda{ |navigation| where(:parent_id => nil, :is_home => 0, :nav => navigation).order("sort_order") }
end

私のhamlは次のとおりです

%ul
 - Page.parent_nav("Attend").each do |page|
 = page_children(page)

基本的に、各ページにliが必要で、ページに子供がいる場合は、すべての子供にliを含む別のulが必要です... ext ....しかし、私のループはどういうわけかオフになっています...私が間違っていることについてのアイデア

4

2 に答える 2

4

page_children再帰的に呼び出すときに指定する引数を変更していません。おそらく次のようなものです(テストされていません):

def page_children(page)
  content_tag :li do
    link_to page.url, "/#{page.url_path}/#{page.url}"
    page.children.each do |child|
      content_tag :ul do
        page_children(child)
      end
    end
  end
end
于 2012-10-21T18:03:30.017 に答える
1

私は書きます(テストされていません):

def render_pages(pages)
  return "" if pages.empty?
  content_tag(:ul) do
    pages.map do |page| 
      content_tag(:li) do
        link = link_to(page.url, helper_to_generate_the_page_path(page))
        link + render_pages(page.children)
      end
    end.join.html_safe
  end
end

= render_pages(Page.parent_nav("Attend"))
于 2012-10-21T19:39:10.697 に答える