2011-07-18 73 views
0

我有郵件(姓名,電子郵件)的字符串:如何使用字符串?

@emails = "Nina Beeu [email protected], Vasilina Korute [email protected], Hikote Ewefs [email protected], 
Egert Erm [email protected], Sambuka Ioas [email protected], Vanish Kiki [email protected], Inoke Xxx [email protected]" 

我需要從這個字符串,字符串:名字,姓氏和電子郵件,並將它們粘貼到表:

<table border=1> 
    <tr> 
    <td> 
     Name 
    </td> 
    <td> 
     Surname 
    </td> 
    <td> 
     Email 
    </td> 
    </tr> 
</table> 

如何我能行?

回答

1
<table> 
    <% @emails.split(", ").each do |chunk| %> 
    <tr> 
     <% ["Name", "Surname", "Email"].zip(chunk.split(" ")).each do |data| %> 
     <td><%= data.join(": ")</td> 
     <% end %> 
    </tr> 
    <% end %> 
</table> 
+0

謝謝,真棒!非常快 –

1
@emails.split(/,\s+/).each do |details| 
    name, surname, email = details.split(" ") 
    # do your html creaty thing here 
end 

更明確地說,你可以在ERB做到這一點:

<table border=1> 
    <% @emails.split(/,\s+/).each do |details| %> 
    <% name, surname, email = details.split(/\s+/) %> 
    <tr> 
     <td><%= name %></td> 
     <td><%= surname %></td> 
     <td><%= email %></td> 
    </tr> 
    <% end %> 
</table> 

而且在HAML變體:

%table(border=1) 
    - @emails.split(/,\s+/).each do |details| 
    %tr 
     - details.split(/\s+/) do |detail| 
     %td= detail 
+0

謝謝你,真棒! –

相關問題