Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Rails how to map name and id from array?

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?

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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, …

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading