Hello friends , I have a clear method here :: def create @invoiceitems = Invoiceitems.new(params[:invoiceitems]) @invoiceitems["invoice_id"] = params[:id] if @invoiceitems.save flash[:notice] = 'Invoiceitems was successfully created.' redirect_to :action => 'new',:id => params[:id] else render :action => 'new' end end In the table invoiceitems I have a field called invoice_id which is a foreign key and the primary key of the table invoices. I need to save the invoiceid which is in the URL itself (params[:id]) before saving .How can I do it . The above method is not storing that . Thanx and regards, Naroor R., www.naroor.blogspot.com
on 2005-12-16 09:33
on 2005-12-16 11:07
Naroor, how did you set up the association in your Invoiceitems model? I suspect either the association is not set up right, or the :id parameter is not coming in with the request. A functional test for this action would be a good idea. (goes for all your other actions as well :) As an aside, it will benefit you in the long to be consistent with plural/singular in naming your identifiers. Consider calling your class Invoiceitem as opposed to Invoiceitems, same for the instance variable. cheers Gerret
on 2005-12-16 11:15
Hi Gerret, class Invoice < ActiveRecord::Base belongs_to :company has_many :invoiceitems end class Invoiceitems < ActiveRecord::Base belongs_to :invoice end These are my model entries .Please let me know what wrong here ? Thanx and regards, Naroor R.
on 2005-12-16 11:22
Gerret , Another try that I made was to create a hidden text box and add the params[:id] to that and save the whole contents .There also it didnt work . Please comment . Thanx and regards , Naroor R.
on 2005-12-16 12:28
Naroor, 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: 1) 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). 2) 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 want. With those two changes done your method will look like this: def create @invoiceitem = Invoiceitem.new(params[:invoiceitem]) if @invoiceitem.save flash[:notice] = 'Invoiceitem was successfully created.' redirect_to :action => 'list' else render :action => 'new' end end 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. cheers Gerret