ruby on rails - Recursion-Printing Parents and Children with indentation(haml) -
i trying print parents' , children's name indentation. not sure how can in view.(haml)
i have model self join:(just give structure, have other attributes etc. think irrelevant)
class post < activerecord::base has_many :children, :class_name => "post" belongs_to :parent, :class_name => "post", :foreign_key => "post_id" end
so has hierarchy , wanna have this(let's assume attribute 'name'):
post 1 post 1.1 post 1.2 post 2 post 2.1 post 2.2
i pretty new in ruby-on-rails. please bear me. really appreciate if quite explanatory answer.
thank much in advance, j
you can utilize recursion. create partial calls itself.
# app/views/posts/_post.html.haml = post.title = post.content = render partial: 'post', collection: post.children
i'm assuming have posts controller comes folder it's views in app/views/posts
. need create partial there called _post.html.haml
calls itself. render
line tells render same partial each of post's children.
the indentation can handled via css.
ruby-on-rails recursion
No comments:
Post a Comment