简体   繁体   中英

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.

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.

Ascaffold 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

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

Here is a tutorial for building API with grape.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM