Scaffold generator: create vs. new

Can anyone supply me with a simple, succinct explanation of what the
difference is between the “new” controller and the “create” controller
that the scaffold generator produces?

I run:

script/generate scaffold modelname controllername

And now I need to do some work inside the controllername.rb file. For
example, pulling a list from another database table to supply for a
drop-down select in the “create a new whatsit” form.

@sprockets = Sprocket.find(:all)

Do I put this in new or create? Why? I’ve been floundering around on
this for some time, and I’ve not been confident that what I’m doing is
the right way, is efficient as far as programmer hours, or will scale
correctly in production.

Thanks all.
–John

Scaffold generator: create vs. new wrote:

@sprockets = Sprocket.find(:all)

Do I put this in new or create? Why? I’ve been floundering around on
this for some time, and I’ve not been confident that what I’m doing is
the right way, is efficient as far as programmer hours, or will scale
correctly in production.

The new and create methods implement actions within the controller.

The new action is called after you choose “new”, and before the form is
displayed to allow you to populate the new object. Note that the object
created in the new method is there to keep the form rendering happy. If
you set any values in that object, they will appear in the form.

The create action is called to take the values from that form, populate
a new object, and save it in the database.

So to add information in the “create” form, you need to retrieve it in
the new method.

regards

Justin

The new action is called after you choose “new”, and before the form is
displayed to allow you to populate the new object. Note that the object
created in the new method is there to keep the form rendering happy. If
you set any values in that object, they will appear in the form.

The create action is called to take the values from that form, populate
a new object, and save it in the database.

So to add information in the “create” form, you need to retrieve it in
the new method.

Ah, so new is called before the form is displayed, and create is called
when the form is submitted, right?

And the same relationship exists between the edit and update actions?
Edit is called before the form is displayed, and update is called when
it’s submitted?

Thanks, this helps a lot.

Steve K. wrote:

Exactly. You’ve got it.

Just remember that the names don’t have any magical meaning; they’re
just the sensible names and the flows someone chose when creating the
scaffold generator. Your own actions and views can be called “turkey”
“gravy” and “cranberry_sauce” if you want, though it’s probably best if
they make sense so you’ll understand your own code six months from now.

Yep. Funny that the scaffold generator gives these ambiguous default
action names, when other Rails things are so much more obvious (like the
various points that validation can be applied, for example.)

Thanks again.

Exactly. You’ve got it.

Just remember that the names don’t have any magical meaning; they’re
just the sensible names and the flows someone chose when creating the
scaffold generator. Your own actions and views can be called “turkey”
“gravy” and “cranberry_sauce” if you want, though it’s probably best if
they make sense so you’ll understand your own code six months from now.

John wrote:

The new action is called after you choose “new”, and before the form is
displayed to allow you to populate the new object. Note that the object
created in the new method is there to keep the form rendering happy. If
you set any values in that object, they will appear in the form.

The create action is called to take the values from that form, populate
a new object, and save it in the database.

So to add information in the “create” form, you need to retrieve it in
the new method.

Ah, so new is called before the form is displayed, and create is called
when the form is submitted, right?

And the same relationship exists between the edit and update actions?
Edit is called before the form is displayed, and update is called when
it’s submitted?

Thanks, this helps a lot.