The gSchool students began our first big Rails project this week. We’re building a restaurant site with online ordering. The project is called Dinner Dash.
In support of learning Rails, Jeff gave a talk on how RESTful Archecture should look in a Rails application. Here are my notes on what he said. I broke it down into CRUD (Create, Read, Update, Destroy) operations.
I’ve shown the HTTP request verbs and routes and the controller methods that match them. It should be noted that GET will display request params in its URL, while POST requests will carry request params hidden in the body of the HTTP request.
For the examples, I used ‘articles’ as a resource. For single records, I referred to an article with an id of 1, although many different ids would actually exist. I also assumed that strong params are defined in a method called ‘article_params’ in the Articles Controller. These patterns came from the Jumpstart Lab Blogger Tutorial.
Setting Up The Router
In config/routes.rb:
1
|
|
Create
Creating A Single Record
- GET ‘/articles/new’ (displays the new article form)
- POST ‘/articles/new’
In app/controllers/articles_controller.rb:
1 2 3 4 |
|
Read
Reading Multiple Records
- GET ‘/articles’
1 2 3 |
|
Reading A Single Record
- GET ‘/articles/1’
1 2 3 |
|
Update
Updating A Single Record
- GET ‘/articles/1/edit’ (displays the edit form)
- PUT ‘/articles/1’
1 2 3 4 |
|
Destroy
Deleting A Single Record
- DELETE ‘/articles/1’
1 2 3 4 |
|
Search
Searching With A Filter
- GET ‘/articles?author_id=6’
Saved Searches
- POST ‘/searches’ (creates the search)
- GET ‘/searches/12’ (accesses the saved search)