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.
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.