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.
    1. Adapters: Describe which attributes and relationships should be serialized.
    2. 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:

This ruby on rails tutorial page provides you the following key areas such as ruby , rail , ruby on rails , rail forum , ruby on rails tutorial , ruby tutorial , rails guides , rails tutorial , learn ruby , rails form_for , ruby rails , ruby class , what is ruby on rails , rails installer , ruby online , learn ruby on rails , ruby on rails jobs , rails find_by , install rails , easyrail , rubyonrails , link_to rails , ruby on rails developer , learn ruby the hard way , railscasts , ruby on rails examples , ruby on rails vs php , rails 4 , rails activerecord , rails generate , ruby and rails , ruby on rails download , install ruby on rails , ruby net http , what is rails , ruby app , ruby vs ruby on rails , ruby on rails windows , rails for zombies , ruby on rails book , ruby on rails development , ruby on rails ide , ruby on rails tutorial pdf

Related Searches to Creating a Simple API with Rails