Topic: silly view problem

I'm sorry for the silly questions.
  The problem is about the invoice/show/id page it was great until i created a second order and invoice, then the
@customer.stuff displayed twice, I created a third order and invoice, hence display's three times. I only want it to diplay once. I thought it might be because invoice belongs_to customer so i tried displaying order.stuff (invoice also belongs_to order) and it only displayed just once.
This is the part of the view;

<% for invoice in @customer.invoices %>
<p><strong>Invoice to:</strong></p>
<p><%= @customer.name %>
<p><%= @customer.address %>
<% end %>

And this is part of the invoice controller;

def show
    @invoice = Invoice.find(params[:id])
    @invoice_item = @invoice.invoice_items
    @items = @invoice.items
    @customer = @invoice.customer
    @order = @invoice.order
end

Am I totally off track? Oh, I mean off the rails?
Any help is very much appreciated.

Re: silly view problem

I guess its really simple like the log says,
show fields from customers where invoices.customer_id = 49
show fields from orders where invoices.order_id = 58
where there is only one invoice for order 58
and three invoices for customer 49
Just not sure how to customize it to suit.
Thanks again.

Re: silly view problem

I'm pretty sure its a "less is more".

In your invoice controller if you do this..

  
def show
@invoices = Invoice.find(:all)
end

you should be all set...(if your relationship stuff is connected..)

then in the view you show the invoices like this:

<% for invoice in @invoices %>
<p><strong>Invoice to:</strong></p>
<p><%= invoice.customer.name %>
<p><%= invoice.customer.address %>
<% end %>

because then each invoice is looped through 1 at a time, and you will be accessing specific information for each invoice
ie. invoice.customer.name

hth

Last edited by chasC (2006-11-10 11:18:08)

Re: silly view problem

You can move the customer info out of the invoices loop so it only displays once. So, instead of this:

<% for invoice in @customer.invoices %>
<p><strong>Invoice to:</strong></p>
<p><%= @customer.name %>
<p><%= @customer.address %>
<% end %>

Do this:

<p><strong>Invoices to:</strong></p>
<p><%= @customer.name %>
<p><%= @customer.address %>
<% for invoice in @customer.invoices %>
  ...
<% end %>

Railscasts - Free Ruby on Rails Screencasts

Re: silly view problem

Oh, thats what i first tried but it didnt work. I must have had the wrong combination in the controller at the time. Thankyou very much for your help.