Saturday, March 13, 2010

REST vs. CRUD

REpresentational State Transfer and describes resources (in our case URLs) on which we can perform actions. CRUD, which stands for Create, Read, Update, Delete, are the actions that we perform. Although, in Rails, REST and CRUD are bestest buddies, the two can work fine on their own. In fact, every time you have written a backend system that allows you to add, edit and delete items from the database, and a frontend that allows you to view those items, you have been working with CRUD.

Resources

You would be used to seeing Rails URLs that look something like /posts/view/2 – which roughly translates to “Please let me view the post with the id number 2″. RESTful resources are very similar, in that they have a controller and (maybe) and id. What they generally don’t have is an action, because it is inherent in the HTTP verb.

To make this work, the Rails team have defined a number of special methods in the controllers that define resources – in this case posts (the .xml bit will become clear in the next part).

Method Resource Verb Explanation
index /posts.xml GET Returns all items
show /posts/1.xml GET Return a single item with id = 1
create /posts.xml POST Create an item
update /posts/1.xml PUT Update item with id = 1
delete /posts/1.xml DELETE Delete item with id = 1

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.