2011-02-02 2 views
0

Я немного застрял и, вероятно, неправильно понял AR. Вот что я получил:Навигация Помощник Активная запись

module PagesHelper 

    def page_loop(pages) 
    output = "" 

    pages.each do |page| 
     output << "<li><a href=\"" << page.title << "\">" << page.title << "</a>" 

     children = page.children 

     if children.size > 0 
     output << "<ul>" 
     page_loop(children) 
     output << "</ul>" 
     end 

     output << "</li>" 
    end 

    return output 
    end 

    def navigation_list 
    parent_pages = Page.where("parent_page_id IS NULL").order("title") 

    output = "<ul>" 
    output << page_loop(parent_pages) 
    output << "</ul>" 
    end 
end 

И тогда следующий в модели:

def children 
    Page.where("parent_page_id = ?", id) 
end 

По какой-то причине он возвращает следующий результат, где у меня есть две страницы, тест, а другой с его parent_page_id, установленным на идентификатор тестовой страницы.

<ul><li><a href="test">test</a><ul></ul></li></ul> 

Так что получать следующие <ul> элементы, но не петлю на страницах.

Я не понимаю методы AR? Я ожидаю элемент <li>.

ответ

0

Gah. Глупая ошибка.

if children.size > 0 
    output << "<ul>" 
    output << page_loop(children) 
    output << "</ul>" 
    end 

Исправлено.