An incorrect development cycle leads into inefficiency and high costs. We know what this means when we work developing mobile technologies, since there is an strict need to coordinate with more teams that deliver services to our applications.
We have came with the idea of mocking and prototyping our Backend services to speed up the development, and work parallelly with the Backend Team without creating a bottleneck until the services are delivered. For this purpose we have been using Apiary, which provides a great service but has two big limitations:
1.- Is not possible to test services that update an object in the server, such as PATCH or POST. We only receive the Mockups.
2.- We cannot filter requests based on different parameters (i.e., the content being sent is always static).
To overcome those limitations we will use Ruby on Rails (RoR). This is a web application framework written in Ruby, and is awesome. It allows you to create and deploy very easily web applications. I find it particularly cool when it comes to prototyping, since creating a web project takes the same effort as just writing the mockups in Apiary! It has many other cool features, such as autoa
We will introduce it with a very short example from a Coursera course. Let’s gonna create a basic blog application where we can store posts, and comments associated to those posts.
NOTE: RoR requires a set up on your computer that I will not cover in this article, but you can easily find instructions on the Internet.
The first step is to create the application with Ruby:
1 |
rails new blog |
This will create a folder with the basic structure for the application.
Our database will have two models: comments and posts. We will use scaffolds to create them:
1 2 |
rails generate scaffold post title:string body:text rails generate scaffold comment post_id:integer body:text |
Those two lines add the entities to our RoR application. We still need to add an association to them (a post can contain several comments). Let’s check in the folder of the application /app/models. There are two files, one for its entity. We will specify here the association between the models.
1 2 3 |
class Comment < ActiveRecord::Base belongs_to :post end |
We will specify also that, if a post gets removed, the comments associated must also be removed.
1 2 3 |
class Post < ActiveRecord::Base has_many :comments, dependent: :destroy end |
Finally, with the following line we will transport those changes into our database schema.
1 |
rake db:migrate |
And launch the application.
1 |
rails server |
By default we can see it in http://localhost:3000/.Wwe can add posts or comments to the URL to display the particular entity. With only a few clicks, we have been able to construct a fully working application, with all the HTTP methods associated to them. If we type in the console rake routes all the routes and Entry Points of the application are displayed.
Follow eenriquelopezTags: development, ruby, rubyonnrails, web