richiepop2 richiepop2 - 1 year ago 55
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:

<% @firm.each do |firm| %>
<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>
<% end %>

How could I get it to list the following:

State 1: NJ

State 2: PA

State 3: DE

Answer Source


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

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