Trouble creating new records


#1

Is it possible to create / save a new record from a controller other
than the one I specified when I created the scaffolding?

My situation is that I have two tables. Emrecs is the parent. Actors
is the child and has a foreign key named emrec_id that references
emrec.id. I scaffolded both, creating controllers and views for each.
My app collects info from the user to fill in the two tables, then
writes the info collected out in an XML file.

Now I need to read in the XML file and put the data back into the
tables.

Any suggestions? Right now it looks like I’m going to have to open a
new REXML doc in each controller. Is that right?

Thanks in advance!

Bill


#2

Bill W. wrote:

Is it possible to create / save a new record from a controller other
than the one I specified when I created the scaffolding?

Absolutely, from any controller anywhere in your app you can simply do

emrec = Emrec.new(:some_param => ‘foo’)
emrec.actors << Actor.new(:another_attribute => ‘bar’)
emrec.save

My situation is that I have two tables. Emrecs is the parent. Actors
is the child and has a foreign key named emrec_id that references
emrec.id. I scaffolded both, creating controllers and views for each.
My app collects info from the user to fill in the two tables, then
writes the info collected out in an XML file.

Now I need to read in the XML file and put the data back into the
tables.

Any suggestions? Right now it looks like I’m going to have to open a
new REXML doc in each controller. Is that right?

You can abstract any common code to a private method in
app/controllers/application.rb. All controllers inherit from
ApplicationController so any methods you want shared between multiple
controllers should go here.


#3

Hi Alex,

Thanks for your reply. It confirms what I thought was the case. It
turns
out I’d made a couple of (compounding) stupid-newbie mistakes. 1) I
wasn’t
populating a field I was validating (so of course the save didn’t happen
;-p ) and 2) I was using a created (as opposed to scaffolded) view and
hadn’t put <%= error_messages_for ‘table_name’ %> in it so I wasn’t
seeing
the error messages that would have steered me to a solution without
wasting
bandwidth on the list. Sorry for the noise. But thanks again for your
reply.

Best regards,
Bill

----- Original Message -----
From: “Alex W.” removed_email_address@domain.invalid
To: removed_email_address@domain.invalid
Sent: Wednesday, May 10, 2006 7:09 PM
Subject: [Rails] Re: trouble creating new records