your model files are correct.
I suggest we clean up the names of your identifies before looking
further at the problem. I think this is where part of the problem
stems from. AFAICT your create method intends to save a single
Invoiceitem, attach it to an Invoice, and then redirect to a screen
where another Invoiceitem may be added.
So I suggest two cleanup steps:
Rename your Invoicesitems class to Invoiceitem. Singular instead of
Plural. That’s the rails convention, and if you follow it, your code
will read much more easily. Same goes for your instance variable
@invoiceitems. Make it @invoiceitem instead.
I suspect you used “./script/generate invoiceitems” to create your
controller. You should pass a singular parameter, so re-run
./script/generate like “./script/generate invoiceitem”. That will give
you a class Invoiceitem instead of Invoiceitems (your table must still
be called ‘invoiceitems’, plural. Which makes sense, because it
contains many items).
In your create method, you are using the parameter :id is the
invoice id. Don’t. In your form, have a parameter :invoice_id, and
don’t use the :id parameter. This makes it very clear which class your
id-parameter belongs to. With your current code, you implicitly
initialize @invoiceitems.id to params[:id], which is not what you
With those two changes done your method will look like this:
@invoiceitem = Invoiceitem.new(params[:invoiceitem])
flash[:notice] = ‘Invoiceitem was successfully created.’
redirect_to :action => ‘list’
render :action => ‘new’
Because we’ve stuck to naming conventions, the line
@invoiceitems[“invoice_id”] = params[:invoiceitem][:invoice_id]
become superfluous, because code to that effect is automatically
performed by Invoiceitem.new(params[:invoiceitem]).
Of course you need to supply a parameter [:invoiceitem][:incoide_id].
In your new.rhtml, you might do this:
<%= hidden_field ‘invoiceitem’, ‘incoice_id’, :value => @invoice.id %>
Good luck, hope this helps. Use functional tests for debugging, its a
real timesave once you’re used to them.