With Ruby and Ruby on Rails we can create and generate full-stack applications. If we want to we can also just use Ruby for the backend or the API with a different front-end and we can use Rails to do this. To start in our terminal we create a new rails app (provided you have rails installed) with the code:
rails new app-name-api -d=postgresql --api
This will generate all the rails files, the added flags of d is to select the database as a PostgreSQL database (Rails defaults to SQLite if this flag isn’t put in) and the api flag is to let rails know that this app is only going to be for an api so it doesn't have to create unnecessary files.
From within the created rails app we focus on the db file, app file, within the app file the models and controllers and the routes file inside config. In these files is where we will create our models for our API. We can create our own models, controllers, db migrations and routes using the ‘rails g (insert model, controller etc.) or we can use ‘rails g resource’ which is a shortcut that will create each of these files for that model. With using resource, however, we have to manually move around some files and change some file names.
For this I will show how to use resource and what changes have to be made to the files. So in the terminal inside the project we call:
rails g resource Api::V1::model_name attributes
we are using namespacing here which is what the Api::V1 is it first to tell us that it is an API and then the V1 is versioning so this would be the first version and as the API needs updating in the future we can create newer versions. Then we obviously have our model name followed by the attributes we wish to have associated with it.
This will create for us the routes with full CRUD for our model. A db migrate file for the model with attributes here we will have to change the name of the migration file by removing the api_vi_, also remove api_v1_ from the create_table, lastly remove ApiV1 from the class. After that is done we can migrate our files but since we chose PostgreSQL as the database we need to create the database first so we run:
After this we go to the model folder we remove the namespacing of Api::V1:: from the class name and move the model out of the Api/V1 folders and delete those folders. For the controller file, we don’t need to make changes as far as the namespacing with api and v1.
The changes we would need to make in the controller would be what we want to be sent when a request is made to our API (add in full CRUD). One simple request would be a get request for all the information from that controller's table so in the controller we could put:
examples = Example.all
render json: examples
this request would return all our ‘examples’ in JSON.
Then to make this api accessible by another app like a frontend app making requests to it we need to uncomment the ‘rack-cors’ gem in the gemfile bundle install the gem. Then within the config/initializers file there is a cors.rb file. Here the code is written out for you, you just need to uncomment it out and change origins to what you want to have access to your api, change this to ‘*’ to give complete access while building it out obviously you can change this later to limit access, requests, types of requests etc.
After that, your rails API is set up and to run it you can simply run rails s in the terminal to start the server. (As of now the database is empty so you can add seed data to get started with some data)
While studying Software Engineering at Flatiron school for projects we created ruby on rails was used as the backend/API and this is how we were taught to build our backend APIs.