Should Pathetic Medium Stats Be A Worry And Concern?

Although some writers on Medium are enjoying thousands of reads for every story they publish, we are not all that fortunate. Those people are likely earning thousands of dollars every month while…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Basics For Building an API In Rails

For my first solo Project in Mod 3(Quarter 3 of 4), as a Turing student, I used Rails and ActiveRecord to build a JSON API, which exposes the SalesEngine data schema. I also learned how to to build Single-Responsibility controllers to provide a well-designed and versioned API, how to use controller tests to drive my design, and used Ruby and ActiveRecord to perform more complicated business intelligence. I will be focusing on building the single responsibility controller and the test driven design behind build it.

Since we are going to be following good TDD(Test Driven Development). Lets begin with the writing our first test to display all the merchants and have them be displayed in JSON.

(This is assuming that the database is already set and you have factorybot setup. If not you can manually create each merchant individually)

Once the test is written now we must build out the route and the the controller. Let’s start with the route first. In our routes file we are going to have to create a namespace route that will give us our desired path.

The controller should be set up like this.

At this point you might be asking well what is MerchantSerializer. First let’s talk about how we get everything to the page. As seen above we call the render method to JSON(json:) then from here is where the serializer comes in. Basically what the serializer does is it will parse the merchant attributes and display them in json form.

It looks like we did get our desired output!!!

This is pretty much the process for any other controllers you want to make. If you make a new route create a new controller for that route. Here is an example of different routes where order did matter for the show and index actions.

Follow these RESTful routes and you can never go wrong. Remember single responsibility controllers goes a long way.

Add a comment

Related posts:

acceptance

The Writing Cooperative and Chalkboard Magnet Poetry Prompt. “acceptance” is published by Molly Skeen in Chalkboard.

I Just Sent My Youngest to Kindergarten and No Part of Me is Sad.

The night before the big day, we packed her lunch and laid out her clothes. We read through the school supply checklist one last time and put everything in her Rapunzel backpack. We read stories…

New Reporting Policies for F1 Student Visa Holders

Students coming into the US with a F1, M or J visa are increasingly feeling the pressure of changing norms instituted by its administration. While it is being done with an intention to stem potential…