开发者

Navigation Helper Active Record

开发者 https://www.devze.com 2023-02-08 09:17 出处:网络
I\'m a bit stuck, and probably not understanding AR properly.Here\'s what I\'ve got: module PagesHelper

I'm a bit stuck, and probably not understanding AR properly. Here's what I've got:

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

And then the following in the model:

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

For some reason it returns the following output, where I've got two pages, test, and another with its parent_page_id set to the id of the test page.

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

So it's getting the next <ul> elements, but doesn't loop over the pages.

Am I misunderstanding AR methods? I'm expecting an <li> element in there.


Gah. Silly bug.

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

Fixed.

0

精彩评论

暂无评论...
验证码 换一张
取 消