why I have one more line?

<table>
  <thead>
    <tr>
      <th>date</th>
      <th>bag</th>
      <th>operator</th>
      <th>district</th>
    </tr>
  </thead>
  <tbody>
    <% @customer.deliveries.each do |delivery| %>
      <tr valign="top" class="<%= cycle('odd', 'even') %>">
          <td>
             <%= delivery.delivered_at %>
          </td>
         <td>
          <% delivery.products.each do |product| %>
            <%= product.description %>,&nbsp;
          <% end %>
        </td>
        <td>
          don't know
        </td>
        <td>
          don't know
        </td>
      </tr>
    <% end %>
  </tbody>
</table>

@customer.deliveries.count give 5 while the table has 6 lines.
Why?
I think it's all ok but I cannot undestand why the each cycle give me
one more line in the table.

Do you mean the table has a total of six rows? If yes then there is
the header row before the 5 data rows.

Colin

No no, including the header I have a total of 7 rows.
If @customer.deliveries.count is 5 then I expect 6 rows including the
header, instead I have 7 rows including the header.

What is in the extra row?
Also I suggest displaying @customer.deliveries.count in the page to
check it really is what you think.
Finally, unless this is rails 3, you should be using
h(product.description) for example, in case there is some html in the
description. Consider what would happen if the description included
</td></tr><tr> for example.

Colin

<table>
<thead>
<tr>
<th>date</th>
<th>bag</th>
<th>operator</th>
<th>district</th>
</tr>
</thead>
<tbody>
<% @customer.deliveries.each do |delivery| %>
<tr valign="top" class="<%= cycle('odd', 'even') %>">
<td>
<%= delivery.delivered_at %>
</td>
<td>
<% delivery.products.each do |product| %>
<%= product.description %>,&nbsp;
<% end %>
</td>
<td>
don't know
</td>
<td>
don't know
</td>
</tr>
<% end %>
</tbody>
</table>

@customer.deliveries.count give 5 while the table has 6 lines.
Why?

Do you mean the table has a total of six rows? If yes then there is
the header row before the 5 data rows.

No no, including the header I have a total of 7 rows.
If @customer.deliveries.count is 5 then I expect 6 rows including the
header, instead I have 7 rows including the header.

What is in the extra row?

here is: <%= @customer.deliveries.count %> this command gives 6

date bag operator district
2011-02-15 secco, umido, plastica, don't know don't know
2011-02-15 secco, umido, plastica, don't know don't know
2011-02-15 plastica, don't know don't know
2011-02-15 secco, don't know don't know
2011-02-15 secco, umido, plastica, don't know don't know
2011-02-15 secco, umido, plastica, don't know don't know
                                                                 don't
know don't know

The rows are 8 including the header.

Finally, unless this is rails 3, you should be using
h(product.description) for example, in case there is some html in the
description.

I'm using rails 3.

Consider what would happen if the description included
</td></tr><tr> for example.

I always have one more row.

well the header does not count, just the row of the database, maybe
the last dont its not a row, maybe is the append contentd of the last
row,
check out well your html format

I think it' s ok.
It' all under <% @customer.deliveries.each do |delivery| %> so if
@customer.deliveries.count gives 5 I'd expect 5 rows.

maybe is the way you show the data why not prove like these
<% delivery.products.each do |product| %>
           <%="Nothing, just to see if 5 row is display it" %>,&nbsp;
         <% end %>

<%= @customer.deliveries.count %>
    <% @customer.deliveries.each do |delivery| %>
      <tr valign="top" class="<%= cycle('odd', 'even') %>">
          <td>
            <%= a+=1 %>
          </td>
      </tr>
    <% end %>
  </tbody>
</table>

@customer.deliveries.count = 5

rows:
1
2
3
4
5
6

clear the console where you are running rails server, then start over
and enter to the view and paste the
result of the consol in pastie.org

maybe is the way you show the data why not prove like these
<% delivery.products.each do |product| %>
<%="Nothing, just to see if 5 row is display it" %>,&nbsp;
<% end %>

If you do not already know how to use ruby-debug to break into your
code then have a look at the Rails Guide on debugging, then at this
point insert
<% debugger %>
and when you run it will break here and you can inspect the variables
and see what is going on.

One possiblity, have you changed @customer or its deliveries since it
was read from the database in such a way that the number of records in
the database (deliveries.count) does not match the current number of
deliveries in the object in memory?

Colin

http://pastie.org/1566058

the result of
SELECT "deliveries".* FROM "deliveries" WHERE ("deliveries".customer_id = 65145
and check out these
<%= @customer.deliveries.count %>
   <% @customer.deliveries.each do |delivery| %>
     <tr valign="top" class="<%= cycle('odd', 'even') %>">
         <td>
   #just to compare what is in the database and what id is bringing to you
           <%= delivery.id %>
         </td>
     </tr>
   <% end %>
</tbody>
</table>

@customer.deliveries.count = 4

<% @customer.deliveries.each do |delivery| %>
      <tr valign="top" class="<%= cycle('odd', 'even') %>">
        <td>
<%= a+=1 %>
<%= delivery.id %>

1 3
2 4
3 5
4 6
5

there is always one more row :frowning:

What does deliveries.size give?

What are the results of breaking in with the debugger and inspecting the data

You have not answered whether you have done something in memory to
deliveries since @customer was picked up from the database (presumably
in the controller). Have you added another delivery in the controller
or view?

Colin

give us the controller and the view

give us the controller and the view

class DeliveriesController < ApplicationController

  respond_to :html
  debugger
  def create
    @customer = Customer.find(params[:customer_id])
    @delivery = @customer.deliveries.build(params[:delivery])
    @document = @customer.build_document(params[:document])
    if @delivery.valid? and @document.valid?
      Delivery.transaction do
        @delivery.save!
        @document.save!
      end
      flash[:success] = "Consegna effettuata."
      respond_with(@customer)
    else
      @products = Product.all
      render 'customers/show', :layout => 'delivery'
    end
  end

end

<%= simple_form_for([@customer, @customer.deliveries.build]) do |f| %>
  <%= render 'shared/error_messages', :object => @delivery %>
  <p>
    <%= f.input :delivered_at, :as => :hidden, :input_html => { :value
=> Date.today } %>
  </p>
  <p>
    <a href="#" id="select_all">Seleziona tutti</a>
      >
    <a href="#" id="deselect_all">Annulla</a>
  </p>
  <p>
    <% for product in @products %>
      <%= check_box_tag 'delivery[product_ids][]', product.id,
(@delivery.products.include?(product) if @delivery) %>
      <%= product.description %><br />
    <% end %>
    <%= f.error :delivery_products %>
  </p><br />
  <%= simple_fields_for @customer.build_document do |doc| %>
    <%= render 'shared/error_messages', :object => @document %>
    <p>
      <%= doc.label :doc_type %>:<br />
      <%= doc.text_field :doc_type, :value =>
(params[:document][:doc_type] if @document) %>
    </p>
    <p>
      <%= doc.label :doc_number %>:<br />
      <%= doc.text_field :doc_number, :value =>
(params[:document][:doc_number] if @document) %>
    </p>
    <p>
      <%= doc.label :issued_by %>:<br />
      <%= doc.text_field :issued_by, :value =>
(params[:document][:issued_by] if @document) %>
    </p>
  <% end %>

  <p class="rightText">
    <%= f.submit 'consegna' %>
  </p>
<% end %>

but i think what Colin want to see, is the index page , where you are
printing the collection

here it is:

<p>
<!--b>Consegne del: <%#= l(Date.today, :format => :long) %></b><br /-->
  <b>Consegne:</b><br />
</p>
<table>
  <thead>
    <tr>
      <th>data ora</th>
      <th>tipo sacchetto</th>
      <th>operatore</th>
      <th>circoscrizione</th>
    </tr>
  </thead>
  <tbody>
    <% @customer.deliveries.each do |delivery| %>
      <tr valign="top" class="<%= cycle('odd', 'even') %>">
        <td>
          <%= delivery.updated_at.strftime("%d-%m-%Y %T") if
delivery.updated_at %>
        </td>
        <td>
          <% delivery.products.each do |product| %>
            <%= product.description %>,&nbsp;
          <% end %>
        </td>
        <td>
          Non si sa ancora
        </td>
        <td>
          Non si sa ancora
        </td>
      </tr>
    <% end %>
  </tbody>
</table>
<p class="rightText">
    <button id="print_button">stampa</button>
</p>

mmmm delvieries.count gives 4 while deliveries.size gives 5.