Paths in Ruby Rack
Tags: ruby, beginners
Published: 2021-09-20 08:51:24

I don't know about you guys but my life has been pretty crazy recently, so lets go over something pretty simple but crucial to backend programming. Today we'll be going over the topic of paths, specifically in the context of Ruby Rack, but the knowledge can be applied more generally as well.

#Paths. What are they?
Well I'm glad you asked, hypothetical person from the internet. Paths are, as the name suggests, the roads through which information travels to and from a server. If a user wants to ask a database how many cats there are in the universe, then they are routed through a path, so that the server knows what they are asking for and from where, and therefore, how to react to that request. After all, showing someone a cat and letting someone post a picture of a cat are two very different things. So, what are some different paths and routes you can take?

##Let's talk cat herding

There are a whole lotta' different ways you can interact with internet cats and we're going to go over a few.

####GET
So what if you want to look at all the cat pictures? `GET` would be your path of choice.
```
if req.path.match(/cats/) && req.get?
   #  gives you cat pics
end
```
So for a quick walkthrough of the above, `req` is short for Rack Request (if you were putting this in your own code you'd need to make sure that's declared) and I'll be using that shorthand from here on. The above code reads, if the requested path matches `/cats/` AND it's a get request, you get given cat pics (# like that indicates a comment in ruby). Sadly, that code doesn't actually give cat pics.

What if you only want to see pictures of tabby cats? Well, for this hypothetical cat haven, all you'd have to do is change `/cats/` to `/cats/tabby`. Paths can change a lot, even if they're the same request, but what about if it's a different request?

####POST
If you want to upload new cat pictures then `GET` simply would not do, but fortunately, `POST` exists.
```
if req.path.match(/cats/) && req.post?
   #  lets you upload cat pics
end
```
Thanks to the code above, you can always add more cat photos to the world. Even though the path is to `/cats/` just like the `GET` request, `POST` allows you do something completely different than the last request. Such is the power of paths. But what if your cat has made a new cat friend and you want to let people know?

####PUT
`PUT` lets you do just that.
```
if req.path.match(/cats/) && req.post?
   #  allows updates to already existing cats
end
```
`POST` and `PUT` are a lot alike, but unlike `POST`, `PUT` allows you to update and change preexisting information and profiles, not just add new ones. Now, I know what you're thinking, what if your arch nemesis Mr. Mittens challenges you to a duel and you sadly have to defeat the fluffy little monster?

####DELETE
`DELETE` allows you to do just that.
```
if req.path.match(/cats/mittens) && req.delete?
   #  enables you to dispatch of your nemesis once and for all
end
```
Truly a powerful tool.

As I said at the start, paths are a very simple, but very crucial part of any good backend. I hope our little cat-venture helped you to understand them a little bit better. Until next time, happy coding and may the cats be with you.