Creating a Rails API from scratch.
Published: 2021-03-30 16:36:21

Once you have your app idea ready to be built out, you need a way for your browser to request information from the server.  This guide will show you the steps needed to set up your own backend, and also where your browser will be pulling out information. 

##Step One

Start a new rails backend folder by using rails new <backend-folder-name> --api.  Don't forget to add the api because you will also need some files that come with it. 

##Step Two

In your terminal you can use rails g resource to make your model, controller, and all associated files that comes with the resource generator. Make sure it's singular. 

rails g resource <Name> attribute:string, attribute:integer, attribute: boolean

##Step Three

Now you must set up your table migrations so in your terminal you run rails db:migrate, and you should see your schema updated.

##Step Four

Next we need to start making some information so your server can pull from the backend.  Inside your db/seeds.rb file you start writing out some seed data associated to your project.  

name = User.create(name: "Gus", last_name: "Bikos")   etc.

There are many different ways to go about creating seed data do whichever way works for you. 

##Step Five

Ruby has a gem that enables CORS (Cross Origin Resource Sharing).  This allows different browsers/servers to share scripts, and exchange data.  Inside your gemfile you must include: gem 'rack cors'.  In your terminal do bundle install to install it.

##Step Six 

Then go to your cors.rb file, config/initializers/cors.rb and uncomment:
```
Rails.application.config.middleware.insert_before 0, Rack::Cors do
  allow do
    origins ‘example.com’
    resource ‘*’,
      headers: :any,
      methods: [:get, :post, :put, :patch, :delete, :options, :head]
  end
end
```

Change origins from example.com to *.  The * allows it's info to be available between all browsers.

##Step 7 

Your routes have a resource <name>, figure out which routes you need and add them to your routes file. 

##Step 8 

Next set up the controller.  Go to app/controllers and set up your index, show , create, update, destroy.  Your front end will take care of the new, and edit since we will be making our forms there. 

**Here we are: 
1) Rendering all data to json
2) No longer need to add .require in private params**

```
class SongController < ApplicationController

    def index 
        @songs = Song.all 
        render json: @songs
    end 

    def show
        @song = Song.find(params[:id])
        render json: @song
    end 

    def create 
        @song = Song.create(song_params)
    end 

    def update
        @song = song.find(params[:id])
        @song.update(song_params)
        render json: @song 
    end 

    def destroy
        @song = song.find(params[:id])
        @song.destroy
        render json: @item
    end 

    private 

    def song_params 
        params.permit(:name, :artist)
    end 

end
```

##Step 9 

Now set up your serializer. This is where you can assign which attributes are displayed on the DOM.  

1) But first we must install the gem. In your gemfile do:  gem ‘active_model_serializers'

2) Run bundle 

3) In your terminal run rails g serializer Song.  Always singular.

4) Start your server by running rails S

5) Then you can add which attributes you wanted displayed in your DOM.  (This also accepts instance methods as well).

6) Go to your config/initializers and create a file 'ams.rb', (or any file name you prefer), and input the following: 

```
ActiveModelSerializers.config.default_includes = '**'
```

This allows all serializers to be able to interact with each other. 

##Step 10 

Then you can set up your associations by building out your next model, and your joiner if you have one as well.

Once again we use the rails g resource <Name>   <attributes>
and for our joiner we need to add our belongs_to.

Then in your models update the macros depending on what type of association you have between your models.  It can he a has_many, has_one, has_many_through, etc.

Also update your serializer and add the associations there as well.

##Step 11

Now you are ready to deploy your backend but first we must seed the data by doing: 

rails db:seed

Once your data is seeded it should show up in your browser with all the information you stored in your seed file.