0

Ok so i have this loop

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

And I keep getting stack level too deep

I am using this gem for my tree structure and my model and haml is the following

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

My haml is the following

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

Basically i want a li for each page and if the page has children then i want another ul with an li for all the children...ext....but my loop is off somehow...any ideas on what im doing wrong

2 Answers 2

4

You're not changing the arguments you give to page_children when you call it recursively. Perhaps something like the following (untested):

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
Sign up to request clarification or add additional context in comments.

1 Comment

This needs some polishing, note that helpers return strings, they do not render by performing side-effects like a ERB view.
1

I'd write (untested):

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"))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.