Thomas
Thomas

Reputation: 723

How to add line number in a rails app?

I writting a simple app in that an item can have multiple line items. When you go into the items detail view you can see all the line items that belong to it.

I'd like to add a line number to the line_items, when I'm in the item detail view I'd like to see

  1. line_item_1
  2. line_item_1
  3. line_item_1

I was thinking about an autoincrement columns, but this would start from 1 when I reimport the data. So maybe a virtual column, but how would I calculate what to display?

Any ideas?

Thanks Thomas

Upvotes: 0

Views: 85

Answers (1)

Benoit Garret
Benoit Garret

Reputation: 13675

You could do:

<% line_items.each_with_index do |item, index| %>
  <%= render :item_partial, :locals => {:item => item, :index => index} %>
<% end %>

item partial:

<%= index %>. <%= item.name %>

If you want to keep the same order every time, just order your line items by creation date.

Upvotes: 2

Related Questions