Ruby on Rails - Creating a Simple API with Rails - ruby on rails tutorial - rails guides - rails tutorial - ruby rails
What is API Rails?
- Rails::API is a subset of a normal Rails application, created for applications that don't require all functionality that a complete Rails application provides.
- It is a bit more lightweight, and consequently a bit faster than a normal Rails application. For example, assets, and frontend things such as CSS and JavaScript.
- Using Rails-API will allow you to utilize the following benefits from Rails.
- URL generation
- Resourceful generation
- Header and Redirection Responses
- Plugins: Many third-party libraries come with support for Rails
- Related to many third-party libraries, we are using in this tutorial Active Model Serializers.
Active Model Serializers:
- Active Model Serializers brings convention over configuration to JSON generation. Basically, it has adapters and serializers.
- Adapters: Describe which attributes and relationships should be serialized.
- Serializers: Describe how attributes and relationships should be serialized.
- Active Model Serializer is under-development version in Rails 5, and Rails 5 will use AMS by default.
Creating our API:
- Our API will be for a TODO List application.
- For this I'm using:
- Rails 4.2.1 (remembering rails-api will be included on rails 5)
- Ruby 2.1.1
- rails-api new todo
- Doing this will create the application without assets/helpers/views. As you can see here
- Add Active Model Serializer in your Gemfile
gem 'active_model_serializers', '~> 0.10.0.rc2'
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - ruby on rails tutorial - rails guides - ruby rails - rubyonrails - learn ruby on rails - team
Creating a model
- In our API, we will have a model called Task.
bash rails g scaffold task title completed:boolean order:integer
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - ruby on rails tutorial - rails guides - ruby rails - rubyonrails - learn ruby on rails - team
Creating a serializer
bash rails g serializer task title completed order
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - ruby on rails tutorial - rails guides - ruby rails - rubyonrails - learn ruby on rails - team
- The serializer should look like:
class TaskSerializer < ActiveModel::Serializer
attributes :id, :title, :completed
end
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - ruby on rails tutorial - rails guides - ruby rails - rubyonrails - learn ruby on rails - team
- Here is one interesting thing about AMS and Serializers: If you have some relationship between models, you can just use a similar Active Record syntax in your serializer: has_many or belongs_to.
- Let's try to test our API and send a request: