There are bread crumbs in the @tracks array, I go through them

 <% @tracks.map do |track| %> 

and inside this code lists are formed (a drop-down menu of breadcrumbs) depending on the conditions of some.

 <% if track[:url] %> <%= link_to( track[:name], track[:url]) %> <% else %> <%= link_to( track[:name], slug_path(track[:slug])) %> <% end %> <ul> <%- childrens.each do |child| -%> <li> <%= link_to(child['name'], child['slug']) %> </li> <% end %> </ul> 

I need to put restrictions on @tracks.count-2 So that the extra ul not displayed, I am empty

  • Can it be easier to convert the \ @tracks into a collection that will not result in the output of an extra empty list? For example, using \ @ tracks.select {...} .each. However, to help in this direction, you will need to tell more about the structure of \ @tracks and what happens in childerns - cheops
  • And why is there a map ? Why not each ? You throw out return value. - D-side
  • Sorry, I forgot to add what I am doing, soyuzno, I do not throw it out, I make a link. I just started learning Ruby, so I am not familiar with some properties and methods. - Marathon
  • one
    No, what you throw out the return value of the map is (for the call is in <% %> , not <%= %> ); and you refer to the link (as a side effect in computer science; how puts returns nil , but prints something on the screen). In theory, there is enough for each . - D-side
  • one
    The only problem is that for an empty children an empty list is displayed? So wrap it in if , checking if the list is not empty ( unless childrens.empty? For example). PS: children is already a plural, the only child for it. - D-side

1 answer 1

to choose

  1. @tracks[0..-2]
  2. @tracks.take(@tracks.count - 2)