This Banner is For Sale !!
Get your ad here for a week in 20$ only and get upto 15k traffic Daily!!!

Simple, beginner steps: API with Rails!




Let’s speak by way of some fundamentals to constructing out an API in Rails.

This assumes you have already got a rails listing began. If not, run rails new [name] --api to start out new. Be sure to have rails put in first by operating gem set up rails!



1. Construct out your assets

For the sake of this instance, we’ll work with the next:

  • Proprietor –< Pets >– Pet Sitters
  • An proprietor can have many pets
  • A pet sitter has many pets they maintain

Let’s construct out our assets. We will use rails g useful resource as a result of it’ll generate mannequin, controller, migration and serializers for us.

rails g useful resource proprietor identify cellphone:integer
rails g useful resource sitter identify cellphone:integer energetic:boolean
rails g useful resource pet identify age:integer sitter:references proprietor:references
Enter fullscreen mode

Exit fullscreen mode

It will construct out our assets, and embrace the overseas keys within the pet migration.



2. Add/verify relationships

Let’s make certain {our relationships} are appropriate. The useful resource generator robotically generated the belongs_to after we used references, however we have to go into the Proprietor and Sitter fashions and embrace:

In Proprietor:

has_many :pets
has_many :sitters, by way of: :pets
Enter fullscreen mode

Exit fullscreen mode

In Sitter:

has_many :pets
has_many :homeowners, by way of: :pets
Enter fullscreen mode

Exit fullscreen mode

Now {our relationships} are confirmed. Within the Pets mannequin you need to see:

belongs_to :proprietor
belongs_to :sitter
Enter fullscreen mode

Exit fullscreen mode

That is additionally a very good time to consider whether or not any relationships will want dependent: :destroy however extra on that later.

3. Validations

Now that our fashions are linked, let us take a look at validations in our fashions. Let’s assume an proprietor have to be 16 or older to make use of the service, and a pet sitter have to be 18 to be a pet sitter. Additionally, a pet can’t be created with no identify.

In Proprietor mannequin:

validates :age, numericality: {greater_than_or_equal_to: 16}
Enter fullscreen mode

Exit fullscreen mode

In Sitter mannequin:

validates :age, numericality: {greater_than_or_equal_to: 18}
Enter fullscreen mode

Exit fullscreen mode

In Pet mannequin:

validates :identify, presence: true
Enter fullscreen mode

Exit fullscreen mode

4. Routes

Let’s check out our routes in Config >> Routes. Since we used the useful resource generator, we’ll see:

assets :homeowners
assets :sitters
assets :pets
Enter fullscreen mode

Exit fullscreen mode

Because of this all default routes are at the moment open. Let’s clear up what’s accessible:

assets :homeowners, solely: [:index, :show, :create, :destroy]
assets :sitters, solely: [:index, :show, :create, :destroy]
assets :pets, solely: [:index, :show]
Enter fullscreen mode

Exit fullscreen mode

We will outline the routes any manner we wish to, however that is what we’ll use for now.

5. Controllers

Now that we have got the routes outlined, let’s head for the Controllers and get among the routes outlined. Let’s simply have a look at an instance of OwnersController:

def index
        homeowners = Proprietor.all 
        render json: homeowners, standing: :okay
finish

def present
        proprietor = Proprietor.discover(params[:id])
        render json: proprietor, standing: :discovered
finish

def create
        render json: Proprietor.create!(owner_params), standing: :created
finish

def destroy
        Proprietor.discover(params[:id].destroy
        head :no_content
finish

personal

def owner_params
        params.allow(:identify, :cellphone)
finish
Enter fullscreen mode

Exit fullscreen mode

Be sure every Controller has routes outlined for every route we have laid out in our Config >> Routes. Technically we did not actually need robust params for this, however I wished to point out an instance.

6. Serializers

Be certain that the serializer gem is within the Gemfile. If it is not, add gem "active_model_serializers", "~> 0.10.12"

Our useful resource generator generated serializers for every mannequin already. We will use these to restrict what we obtain in our responses. Something within the associated Controller will default to the serializer with the matching identify, except you specify in any other case. In our OwnerController, that will appear like:

render json: <one thing>, serializer: <CustomSerializerName>
Enter fullscreen mode

Exit fullscreen mode

The Article was Inspired from tech community site.
Contact us if this is inspired from your article and we will give you credit for it for serving the community.

This Banner is For Sale !!
Get your ad here for a week in 20$ only and get upto 10k Tech related traffic daily !!!

Leave a Reply

Your email address will not be published. Required fields are marked *

Want to Contribute to us or want to have 15k+ Audience read your Article ? Or Just want to make a strong Backlink?