Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
REST and Resources2:24 with Jason Seifer
REST stands for Representational State Transfer. REST is the Rails way for operating on resources on your site. This video explains what REST is and how we'll use it to operate on the User Friendship resource in our application.
The verbs in REST map very well to Rails routes:
GET => new, show
POST => create
PUT => update
DELETE => destroy
In our last stage we set up the friendship between users and other users.
We did this by creating models and setting up the association between users and friends.
In this stage, we're going to use those relationships to create
the front end for friendships on our site.
We'll start by adding a button on a users profile page to create a friendship.
Before we jump into coding this though,
we should take a moment to understand how all of this works.
Let's take a look at the process of creating a friendship.
We can consider the friendship that we create, our user friendship model, a resource,
and keeping with that analogy, when we add a friend we're creating a user friendship.
We can represent this in a rails application by using a concept called REST,
which stands for representational state transfer.
REST works with resources and URLs
depending on the URL and the way that we request a page
that will create or modify a resource.
That process is called with something that we refer to as an HTTP verb.
There are 4 verbs that we need to go over:
Get, post, put, and delete, and they all work on resources a certain way.
A get request will display the resource and generally won't change it.
A URL for this might look something like /users/jason.
A post request will create a resource.
This is like when we sign up for the site and click the submit button.
When we update a resource, that verb is put,
and we're making changes to an existing resource.
And when we don't want a resource anymore, we use a delete request.
These all happen to map very well to database calls.
Post works with creating something in a database,
put with updating, get with reading, and delete with destroying.
Now that we have an understanding of how the concepts work with REST,
let's go ahead and set up a resource for user friendships in our application.
You need to sign up for Treehouse in order to download course files.Sign up