REST API on RoR

I decided to learn to implement REST Api.. so i chose Ruby on rails framework for this purpose. In this post i will explain building rest api backend using ruby on rails. I am working on MAC env. I suggest u select a linux env or mac when working with RoR, as it is more convenient and working with console is always cool.

You can follow my previous blog post for ruby on rails installation using the below link.

Ruby on Rails Installation

Follow this link to read more about RESTful API.

I wanted to create a simple dashboard with basic CRUD api calls. To create a new task, update, delete or show all tasks we will have 4 different api calls, i.e. GET,POST,PUT,DELETE.

I will share each api call functionality one by one. Now as ruby on rails is a MVC model. So our API call will hit the controller using the routes.rb file and will modify or fetch the data from database using models and render the view as response to be sent to the caller.

I ran into some gems that were needed for my requirement. Go to your Gemfile and add these lines

# rake gem
gem ‘rake’, ‘~> 10.4.2’

Rake gem is used to experiment with the migrations and do many more things like running cron_tasks. You can read more about rake here.

As i said earlier rails app requires 4 steps that of routes, controller, model and view. So setting the route for our application. Go to routes.rb file and add this line

resources :tasks

‘tasks’ is the name of my application which will be accessed like this ‘localhost:3000/tasks’. This url will hit the controller tasks_controller.rb and will render the index.html.erb inside tasks folder in views directory of app. So we need to generate the appropriate controller and model for this app.

rails generate controller tasks #will generate the controller tasks_controller.rb

rails generate model task #will generate the model task.rb

Now you will be able to see the files created in your app folder. To have a more subtle view of routes run 'rake routes' in your console. This will provide you the path that rails will follow depending on the request.

Ruby on Rails Installation

Ruby on rails is a MVC model framework. which is very popular in recent time and used in many companies. For installation follow the below steps. These are written for MAC env.

  1. Install xcode.

xcode-select –install

2.  Install RVM

curl -SSL https://get.rvm.io | bash

source /Users/<username>/.rvm/scripts/rvm

rvm install 2.0.0

Note: You might have to install bundler gem

command: gem install bundler

3.  Install Git

brew install git

4.   By default rails uses sqlite as its database. But i am using mysql db for this                purpose.

brew install mysql

5.  Now all you need to do is to select a working directory and run the following command.

rails new myapp –d mysql  —- use your appname instead of myapp

gem install bundler

6.  Edit the database.yml file inside myapp/config/ directory

add the username and password for mysql. I suggest you download sequel pro for providing a UI to your database and save the overhead for writing out queries. Choose a db name in the development section in database.yml file. and you are good to go.

7.  Start the mysql server

mysql.server start

8.  Now for creating the database you need to use rake command.

rake db:create

if you receive an error in this step. Go to Gemfile file and replace the line gem ‘mysql2’

with

gem 'mysql2', '~> 0.3.18'

and run the command again

9.  Now all you need to do is to start the rails server.

rails s -p “port_number”

without quotes