richiepop2 richiepop2 - 4 months ago 8
Ruby Question

how to publish second, third, or fourth item from an observation

If I had a multiple items in one observation in a data frame, can I publish the second or third item as stand alone piece of data? For example, let's say my data frame looks as follows:

Name State_of_Business
Company X NJ,PA,DE


and let say I want to print state_of_business with the following view code:

<body>
<% @firm.each do |firm| %>
<tr>
<td> <b>State 1</b> <%= firm.state_of_business %> </td>
<td> <b>State 2</b> <%= firm.state_of_business %> </td>
<td> <b>State 3</b> <%= firm.state_of_business %> </td>
<tr>
<% end %>
</table>
</body>


How could I get it to list the following:

State 1: NJ

State 2: PA

State 3: DE

Answer

Try:

<% @firm.each do |firm| %>
<tr>
    <% firm.state_of_business.split(',').each_with_index do |state, index| %>
        <td> <b>State <%= index + 1 %></b> <%= state %> </td>
    <% end %>
</tr>
<% end %>

PS: Don't forget to add the opening/closing tags, </table> have no matching <table>.