Home > Back-end >  Rails how to map name and id from array?
Rails how to map name and id from array?

Time:12-12

If I am getting parent nodes via and outputting them as a sort of bread crumb list, how do I map both the name of the classification and id to bu8ild a link_to?

<% @parents.map(&:name).each do |parent| %>
    <%= link_to parent, new_sr_path(class_id: parent, sub: :true), class: "form-control" %><svg ...</svg>
<% end %>

@parents = @classification.ancestors.to_a.reverse if params[:class_id].present?

CodePudding user response:

You should iterate through your objects, instead of the names of your objects (generated by the .map(&:name) call):

<% @parents.each do |parent| %>
  <%= link_to parent.name, new_sr_path(class_id: parent.id, sub: :true), class: "form-control" %>
<% end %>

NOTE Having a real instance give you the ability to call all instance methods defined on that object: parent.name, parent.id, ...

  • Related