DIENG HIGHLAND

Dieng Plateau is an active volcanic area in Central Java, which is included in the Banjarnegara and Wonosobo Regencies. It is located west of the Mount Sindoro and Mount Sumbing complexes. Dieng has…

Smartphone

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




Coding your first RESTful Rails API?

Understand these concepts before beginning to ensure smooth sailing.

RESTful APIs use HTTP requests to GET, PUT, POST and DELETE data. These verbs provide the meaning for the request, and correspond to the CRUD actions Create , Read , Update , Destroy . Rails has five RESTful methods pre-built that correspond to HTTP requests and CRUD actions, shown below.

Method: Index // Action: Read // Request: GET

Method: Show // Action: Read // Request: GET

Method: Update // Action: Put // Request: UPDATE

Method: Create // Action: Create // Request: POST

Method: Destroy // Action: Destroy // Request: DELETE

With this simple and uniform structure already in place, why wander from it?

Example: We want our API to return a subset of the merchants with a status of "active" from our database.

Rather than create a separate active method within the Merchants controller to return the list of objects…

We can create a controller to handle to search.

Our controller looks like this:

Utilizing this framework for your controllers will keep your code shorter and easier for other developers to understand. Keep in mind, there may be instances abstracting controllers further could be appropriate — but wait until there is a legitimate need due to your program’s logic.

2. Live and breath your routes.rb file

Do you understand all the ins and outs of the routes.rb file?

The routes.rb file encompasses all of the directions that tell our application where to go to provide a response based on a received request, so its important to fully understand how to correctly write the routes you need.

3. Remember to follow MVC

4. Love your Data

Spend some time with it, get to know it inside and out.

Add a comment

Related posts:

Connecting Methods Through Narrative

We hosted a week-long workshop during the frame of AcrossRCA, in which participants were introduced to cross-disciplinary mindsets and methods through a collaborative analysis of methods and creative…