Writing each loop inside HAML and Rails

I have a HAML:

- @my_patients_list.each do |patient| %tr %td= patient.name %td= patient.date_of_birth %td= patient.gender %td= patient.brand_name.name %td= patient.generics[0].name 

note the line %td= patient.generics[0].name I cheat and print only the first generic. But each panetta can have more than one tribal. So this is a question or another for-each loop for this part. But this HAML I just started using it and still not used to it. Can someone help with this extra loop that I have to write in HAML format, so I can replace it with patient.generics[0].name ? Probably just separate the names of your generics with a comma.

+4
source share
2 answers

Assuming generics is a string array and you want to separate it from the new td ...

 - @my_patients_list.each do |patient| %tr %td= patient.name %td= patient.date_of_birth %td= patient.gender %td= patient.brand_name.name - patient.generics.each do |gen| %td= gen.name 

Otherwise, if you just want to separate the comma ...

 - @my_patients_list.each do |patient| %tr %td= patient.name %td= patient.date_of_birth %td= patient.gender %td= patient.brand_name.name %td= patient.generics.map(&:name).join(',') 
+7
source

If you want to separate the generic name with a comma, you can try the following:

 - @my_patients_list.each do |patient| %tr %td= patient.name %td= patient.date_of_birth %td= patient.gender %td= patient.brand_name.name %td= patient.generics.map(&:name).join(',') 

It will print all related common names, separated by a comma.

+4
source

Source: https://habr.com/ru/post/1498441/


All Articles