A basic use of NameSpacing
Published: 2023-02-21 19:38:24

NameSpacing was a little endeavor I decided to take upon myself for our Phase 4 project at flatiron, let me explain just a couple of highs and lows of this fun and useful feature of rails. 

NameSpacing is a useful tool to prefix the Url path to specified resources. Which will allow your machine to try and locate your controllers etc. under a module of the same name as the namespace.

Yeah it sounds pretty technical but in practice it is a pretty simple set up. when first starting out I went to my Routes.rb file in my backend 
![Route.rb](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eq5kv8a3wi71xq9kx4db.png)

now what you'll need to do in this file is just add a couple very short lines of code underneath the first **do** of the file like so.
![route.rbFile](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mdbwycgna9327v2hmf3l.png)

**DO NOT FORGET TO ADD THE CORRESPONDING END FOR THE DO's**

in the end your routes file should resemble something like this.

![RoutesFile](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ulm9k763d18esznv6h7j.png)

Now your routes are done you need to make sure your controllers also reflect the use of api and v1. what you will need to do next is create two new folders and rest them inside the controllers folder. Name the first folder api and move it into your controllers folder, then create the second and move it inside the api folder. your files should reflect this. 
![api/v1 file](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cxwu0jvmi8kehnsje8f1.png)
then drag and drop your controllers into the api/v1 folder. **MAKE SURE** that your controllers are landing inside the correct file or you will run into bugs that just don't seem to make sense but it is because your computer will be looking for files in the wrong spot. 

And to answer your question, no it is not finished. Just a couple more additions to your controllers to make sure you are establishing the communication correctly! 

Now in each controller, excluding the Application Controller and your Fallback Controller (if you have one established). Since they all now sit under this api/v1 folder combination you need to have your code reflect that as well. at the top of your controllers where you see the 

```
Controller < ApplicationController
```
you will need to add the proper NameSpacing, it should look something like this. 

```
API::V1::Controller < API::V1::ApplicationController
```
And there you have it! your controllers are properly NameSpaced! Now there are other methods of NameSpacing such as use Modules, which require a little bit more code to be written so I wont go into to detail but if you'd like to read up on it (which I recommend) please check out the links at the end of this blog! 

Now in the front end what you will need to make sure you do is include an "/api/v1" to the routes for your fetch calls! heres an example. 
![frontend fetch](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ive5p3jnetg2rwhse5k9.png)

See its not so daunting a task and actually is something relatively simple to set up, now it does come with its own forms of complications and bugs so do take time to read up on it and make sure you understand how to properly implement this into your projects! 

I hope this was informative and helpful in you establishing NameSpacing for yourself!

[Scope vs NameSpacing](https://devblast.com/b/rails-5-routes-scope-vs-namespace)
[NameSpacing with Modules](https://boykoc.github.io/ruby/rubyonrails/2016/12/16/namespaces-and-modules-in-rails.html)