Member-only story
Starting a Rails back end from scratch.
Looking to create a Rails backend from scratch? In this following quick start guide, we’ll cover creating a new API backend, the database and migration, model, and controller files.
From the console we can run
rails new myappname --api
Alternatively, if you are looking to use postgresql as your database, we can run
rails new myappname --api --database=postgresql
Creating resources
A quick way to create your controllers, models, migrations etc, is to use “rails g resource”
rails g resource
#can also type out rails generate resource
Let’s say we have a User model. We can create all the files needed with one line.
rails g resource User name:string email:string password: string
This will create
users_controller.rb in controllers
user.rb in modelsalso this will create the migration file in your db>migration folder
After you create all your models we can run
rails db:migrate
This will run your migration files.
Now your ready to create your seed data and set your relationship in your model files.