DEV Community

Tate Handley
Tate Handley

Posted on

A basic use of NameSpacing

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

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

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

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

RoutesFile

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
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
Enter fullscreen mode Exit fullscreen mode

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

API::V1::Controller < API::V1::ApplicationController
Enter fullscreen mode Exit fullscreen mode

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

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
NameSpacing with Modules

Top comments (0)