It is for Rails scaffold, but it more or less describes how things are done in Rails.
First, there is nothing yet.
For the Create in CRUD:
We need a form to create a record (unless somebody directly use a program to send an HTTP request to our server). The form is generated by:
the above generates a form in HTML. It is a GET /foos/new
nothing special there.
Note that for this form, when a user enters all the details, and click the "Create" button, it POST to /foos/ that is, it POST to www.mysite.com/foos/
the Rails's FoosController#create action will handle it.
the HTTP request is:
POST http://www.mysite.com/foos/ HTTP/1.1
Near the end of this create action, it redirects to http://www.mysite.com/foos/1
This is a GET /foos/1
and it is for the Retrieval in CRUD.
It is handled by FoosController#show action
GET http://www.mysite.com/foos/1 HTTP/1.1
For the Update in CRUD:
To edit something, we need a form, (unless using a program to directly submit the changes)
it is just a GET, handled by FoosController#edit action
when user clicks Update button, it sends
POST http://www.mysite.com/foos/1 HTTP/1.1
The Rails development log says it is a PUT, but Fiddler on Windows shows that it is a POST
the POST variables are
it is handled by FoosController#update action.
After the update, it redirects back to
to show you the record again.
Now the Destroy in CRUD:
The Destroy link is in /foos which shows all the records for foo
Click on Destroy, and it does a POST http://www.mysite.com/foos/1 HTTP/1.1
with POST variables
so it is not a DELETE. It is handled by FoosController#destroy
|Which of CRUD||to prepare the form||controller and action to prepare the form||when the real action of CRUD is happening||controller and action|
|C||GET /foos/new||FoosController#new||POST /foos/||FoosController#create|
|U||GET /foos/edit||FoosController#edit||POST /foos/1 _method=put||FoosController#update|
|D||GET /foos/||FoosController#index||POST /foos/1 _method=delete||FoosController#destroy|
This is tested again using Ruby 1.9.2 and Rails 3.0.5, and the Update and Delete are done the same way: using a POST, with _method=put or _method=delete.