Collections vs Single Resource 0:00In the last episode, you began learning about resourceful controllers and the seven restful actions. We keep coming back to that term, rest. Okay, let's talk about it a bit more in a practical sense. So here I have two routes. If I visit slash articles, that should allow me to read a list of articles, sort of like a collection. But if I visit slash articles slash some identifier, that should allow me to read a single article, right? Okay, so let's think about it.single article, right? Okay, so let's think about it. Slash articles is for a collection, and slash articles slash some kind of identifier is for a single article. And you'll see this convention all over the place. So for example, imagine you are an administrator and you want to look at all users on your site. All right, well, you may have an endpoint of slash users. But what if you need the details of the user with an ID of 20? Users slash an identifier.But what if you need the details of the user with an ID of 20? Users slash an identifier. Once again, show a list of users or a single user. And again, this will work for anything. Are you working with projects? Are you working with invoices? As long as you can find the nouns, you're all set to go. So what's interesting about this is everything you need to know is included as part of the request. And this is unique compared to alternative approaches. REST Stateless Requests 1:22request. And this is unique compared to alternative approaches. For example, if you weren't using REST, you might use the session to pass state from request to request. But with this approach, everything is stateless. Or in other words, everything you need to know, again, is included as part of the request. But now you're probably still suspicious because you're thinking, okay, this is great for reading articles, but it's still not enough. What about other requirements and actions? For example, what about I need to delete an article? Avoid Verbs in URIs 1:47What about other requirements and actions? For example, what about I need to delete an article? What do I do there? Do I do slash articles slash an ID slash delete? Or what if I need to update that article? Would it be articles ID update? And actually, the answer is no, you wouldn't take that approach. So as a general rule of thumb, try not to put verbs in your URI. Instead, we'll communicate intent by using the HTTP verbs. And you're probably familiar with these even if you don't know it. Using HTTP Verbs 2:12Instead, we'll communicate intent by using the HTTP verbs. And you're probably familiar with these even if you don't know it. Get, post, put, delete. You might even know patch. And there's even a couple others. But these are the main ones to consider. And in fact, we could even shorten it a little bit more for practical purposes and stick with this. So get is for reads. Very simple.So get is for reads. Very simple. You can throw in lots of terms about them being idempotent and all that, but keep it simple. It's just for reading stuff like you're loading it in a browser. So all of these would be get requests. Get me a list of articles. Get me a single article. But what about these examples down here when I wanted to update? Okay, well instead, we're going to submit not a get request, but a put request to updateBut what about these examples down here when I wanted to update? Okay, well instead, we're going to submit not a get request, but a put request to update the article with a given ID. Now again, put and patch, there's a lot of overlap and there's a lot of debate and there's a lot of disagreement over when you use one or the other. From a practical standpoint, it doesn't really matter that much if we're being honest. Just pick one. Later, you can learn the unique differences and when you might reach for a patch over a put. But until then, keep it simple.a put. But until then, keep it simple. Okay, and then finally, this one where you might want to delete an article. Well, in that case, you would do a delete request. And finally, this last one, post, you're likely familiar with. If you've ever submitted a form before, it's most often used for creating a new resource. So for example, if I submit a post request to slash articles, that's saying I want to create a new article. Okay, so now think about it. If we combine the HTTP verb with a URI structure, we can handle just about any operation youOkay, so now think about it. If we combine the HTTP verb with a URI structure, we can handle just about any operation you need. And it's not a coincidence, by the way, that get, post, put, and delete nicely map over to the various CRUD actions. It's not a one-to-one, but again, from a practical, simple standpoint, you can think of them in that way to get up and running. So I'll let you take a look at these. And again, this will map over to anything. Let's do another one together. CRUD Examples with Videos 4:17And again, this will map over to anything. Let's do another one together. What will be our resource? How about videos like Larrakass? Okay, let's say you want to look at all the videos on the website. Okay, we might do a get request to slash videos. That will show me a list of videos. But how about the video with a ID of two? Okay, videos slash two. Now let's say I'm the administrator and I want to update the video with an ID of two.Okay, videos slash two. Now let's say I'm the administrator and I want to update the video with an ID of two. All right, well, I will submit a put request to videos slash two. Now I'm not reading that page, I am updating it. And then finally, let's say years go by and that video is now obsolete. All right, let's submit a delete request to videos slash two to delete it. All right, two last things and then I'll let you go. So what about other things like showing a form to create a new video? We talked about that a bit in the previous episode. No problem there, just append to the URI.We talked about that a bit in the previous episode. No problem there, just append to the URI. So if I were to say get videos to edit, that should display a form to edit the video with an ID of two. Or what about showing a form to create a brand new video? All right, same thing. Make a get request to slash videos slash create. These are basic conventions that you'll see across countless frameworks. So think about it. If we visit this URI, it'll show me a form to create a new video. Handling Non-CRUD Actions 5:38So think about it. If we visit this URI, it'll show me a form to create a new video. And when I submit that form, it should make a post request to store that new video. Okay, one more note and then in the next episode, we'll talk about forms and we'll put all of this learning to work. For my experiences, this all makes sense until you need to do something slightly different. For example, maybe you want to subscribe to all videos. Okay, well, what do you do there? What is the URI structure look like? Are you making a get request to get videos slash subscribe?What is the URI structure look like? Are you making a get request to get videos slash subscribe? Well, subscribe is a verb and I thought we learned we really shouldn't use verbs, but we don't know what verb to use for subscribe. Are you creating? Are we updating the video? No, are we deleting? What do we do? And it gets a little tricky, right? So in situations like this, just take a step back and think about what it is you're workingAnd it gets a little tricky, right? So in situations like this, just take a step back and think about what it is you're working with here. If we break it down at its core, we want to create a new subscription. Okay, so with that in mind, we could do this in a number of ways. We could submit a post request to a new resource like subscriptions or you may nest it. So something like this, and that's a key thing to understand. All of this is flexible. You're in control. So nobody's going to beat you up if you tweak things a little bit.You're in control. So nobody's going to beat you up if you tweak things a little bit. That's just the way real life goes. So with this approach, you might hit a video subscriptions controller. And now we have a brand new concept for our code base. So there's almost always a way to stick to this convention if you take a step back and think. All right, so in the next episode, we're going to put all of this to the test. You've learned about resourceful controllers. You've learned about routing. You've learned a little bit about REST.You've learned about routing. You've learned a little bit about REST. But what's the full A to Z here? How do we create a form that submits, for example, a delete request to remove a video? That's the final piece of the puzzle.