Chris T
2006-Feb-22 18:21 UTC
[Rails] Newbie seeks helps ordering ancestors from acts_as_tree
I''m sure this is really obvious but I''m feeling my way here with rails (and Ruby) and have tried a couple of things without success. So, can anyone tell me the best way to reverse order the retuen list of ancestors. In the controller I have: @ancestors = @category.ancestors which returns an array starting at the parent and ending with the root. When I then loop through the array to create a breadcrumb list: <% @ancestors.each do |ancestor| %> <%= link_to h(ancestor.name), :action =>"show", :id => ancestor.id %> > <% end %> I get the breadcrumb list but in the wrong order: parent > grandparent > root Should I be changing the order in the controller or the loop. If so, how (zero Ruby skills, as you can prob tell -- pickaxe book on order!) Thanks Chris T -- Posted via http://www.ruby-forum.com/.
William LeFevre
2006-Feb-22 19:43 UTC
[Rails] Re: Newbie seeks helps ordering ancestors from acts_as_tree
> When I then loop through the array to create a breadcrumb list: > <% @ancestors.each do |ancestor| %> > <%= link_to h(ancestor.name), :action =>"show", :id => ancestor.id %> > > <% end %> > > I get the breadcrumb list but in the wrong order: > parent > grandparent > root >Try using the Array method reverse or reverse!. I''m pretty new myself but I think you can string your methods together and do <% @ancestor.reverse.each do |ancestor| %> Check out the online Ruby documenation, http://corelib.rubyonrails.org/. I use them alot. William -- Posted via http://www.ruby-forum.com/.
Chris T
2006-Feb-22 19:58 UTC
[Rails] Re: Newbie seeks helps ordering ancestors from acts_as_tree
William LeFevre wrote:> >> When I then loop through the array to create a breadcrumb list: >> <% @ancestors.each do |ancestor| %> >> <%= link_to h(ancestor.name), :action =>"show", :id => ancestor.id %> > >> <% end %> >> >> I get the breadcrumb list but in the wrong order: >> parent > grandparent > root >> > > > Try using the Array method reverse or reverse!. I''m pretty new myself > but I think you can string your methods together and do > > <% @ancestor.reverse.each do |ancestor| %> > > Check out the online Ruby documenation, http://corelib.rubyonrails.org/. > I use them alot. > > WilliamDoh! Yup that worked great. Obviously didn''t look properly in the ruby docs. Many thanks! Chris -- Posted via http://www.ruby-forum.com/.