Topic: I need help with select and index

Thought maybe beginning rails was a more suitable place for me:)
Anyhow I have this code to create the invoice_items for the invoice, which i learnt from the excellent tutorials here.

<%= start_form_tag :action => 'create', :id => params[:id] %>

<% @invoice.invoice_items.each_with_index do |invoice_item, index| %>
   <% fields_for "invoice_items[#{index}]", invoice_item do |f| %>
     <p>Line:quantity:<%= f.text_field :quantity %></p>
   <% end %>
<% end %>
   <%= submit_tag 'Create' %>
<%= end_form_tag %>

anyway i need to put in select tags for each invoice_item(line) to choose a product. Like this;
    <select name="invoice_item[item_id]">
      <% @items.each do |item| %>
      <option value="<%= %>">
        <%= item.product %>
      <% end %>

But how do i integrate it. I am completely lost on this one.

Re: I need help with select and index

The collection_select helper is probably what you want. You can use it like this:

<b>item:</b><br />
<%= f.collection_select :item_id, @items, :id, :product %>

This assumes there is an @items array of models already set, that the Invoice belongs_to :item (it has an item_id column) and that the items table has a string product column.

Does that work for you?

Railscasts - Free Ruby on Rails Screencasts

Re: I need help with select and index

Yes. It did. Its amazing only one line! Thankyou once again. I am so glad i asked.