user3342460
user3342460

Reputation: 71

how to build rails api with postgresql

I know nothing about PostgreSQL and a little about Ruby on Rails. I know Ruby. I have experience with the command line.

I'm interning and I was told to build a Rails API that loads data into a two-dimensional array from PostgreSQL database. The API is meant to have methods to get certain pieces of the data given a certain index.

I don't know how/where to begin despite how simple it should be. I have no experience with databases. Please point me in the right direction to get this set up and started.

Upvotes: 4

Views: 3783

Answers (3)

mmsilviu
mmsilviu

Reputation: 1451

Starting with Rails 5, you can generate a Rails API only application. Just use the following command:

rails new project-name-here --api --database=postgresql

I assume that you have ruby, rails and postgresql installed.

rails db:create # creating DB

Running rails generate scaffold User first_name:string last_name:string, it will generate all files needed for User model.

A scaffold in Rails is a full set of model, database migration for that model, controller to manipulate it, views to view and manipulate the data, and a test suite for each of the above.

rails db:migrate # migrating the DB
rails s # starting the server

Now, you can access the /users endpoint. Do not forget to update controllers actions with relevant data.

A detailed example

Upvotes: 11

Murtza
Murtza

Reputation: 1416

You can use rails-api gem. This gem has very helpful documentation but still if you need help, you can ask me in comments.

Upvotes: 0

Emu
Emu

Reputation: 5905

You can user Grape gem for building API. They provide a good documentation also.

Here is a tutorial for building API with grape.

Upvotes: 0

Related Questions