Rails new vs create

sent-hil picture sent-hil · Mar 18, 2010 · Viewed 92.6k times · Source

Why is there a need to define a new method in RESTful controller, follow it up with a create method?

Google search didn't provide me the answer I was looking for. I understand the difference, but need to know why they are used the way they are.

Answer

Steve Weet picture Steve Weet · Mar 18, 2010

Within Rails' implementation of REST new and create are treated differently.

An HTTP GET to /resources/new is intended to render a form suitable for creating a new resource, which it does by calling the new action within the controller, which creates a new unsaved record and renders the form.

An HTTP POST to /resources takes the record created as part of the new action and passes it to the create action within the controller, which then attempts to save it to the database.