Build a bulletin board API with authentication and authorization with Rails 6 # 1 Environment construction

Introduction

I created this because I didn't find many tutorials on Rails API. It's been about half a year since I started using the Rails API, so please point out any mistakes.

I'm going to build a bulletin board API using Rails API mode. The front end is not included in this tutorial. After completing the API, please choose the one you like.

table of contents

It is a total of 18 serializations

Target audience

Without a minimum knowledge of Rails CRUD operations, it can be difficult to catch up.

Final environment ・ Gem

AWS Cloud9 PostgreSQL 9.5.15 Ruby 2.7.1 Ruby on Rails 6.0.3.2

Gem Serializers: active_model_serializers Authentication: devise_token_auth Authorization: pundit Dummy data: Faker Static code analysis: rubocop Tests: RSpec, FactoryBot

Build Cloud9 environment

First, if you don't have an AWS account, get one. In this article, we will explain on the assumption that you have an account.

Find Cloud9 in the AWS Management Console. s1.png

Press Create environment. s2.png

Create it with a descriptive name and description. s3.png The second page is basically all fine by default.

It will take a few minutes to build, so leave it alone. When completed, this IDE screen will be displayed.

s4.png

Ruby version upgrade

Ruby and Rails are included from the beginning, but the version is old.

$ ruby -v
ruby 2.6.3p62 (2019-04-16 revision 67580) [x86_64-linux]
$ rails -v
Rails 5.0.0

s5.png

The stable version at the time of writing the article is 2.7.1, so let's raise it.

$ rvm install 2.7.1
...
$ rvm use 2.7.1
$ ruby -v
ruby 2.7.1p83 (2020-03-31 revision a0c7c23c9c) [x86_64-linux]

It went up safely.

Reference: [AWS] How to update the Ruby version on Cloud9 (using rvm)

Raise Rails version

Again, the latest version is 6.0.3 at the time of writing, but the installed version is 5.0.0, which is quite old, so let's raise it.

$ gem install rails
...
$ rails -v
Rails 6.0.3.2

It's easy.

Reference: Change Rails version in Cloud9

Insert postgreSQL

Why put postgres at this timing?

The default development DB of Rails is SQLite, but in the Cloud9 environment of Amazon Linux, the version of SQLite is low and applications cannot be created. Since it is difficult to upgrade the version, I will use PostgreSQL this time.

Also, as the name suggests, SQLite is functionally lite, so there are many things that can not be done compared to other RDBMSs, and it may be unnecessarily clogged, so avoiding it is also one of the factors.

Install it first.

$ sudo yum install postgresql95-devel postgresql95-server postgresql95-contrib
$ psql --version
psql (PostgreSQL) 9.5.15

Next, initialize and start, user creation. You can ignore Permission denied.

$ sudo service postgresql95 initdb
Initializing database: sudo service postgresql95 start     [  OK  ]
$ sudo service postgresql95 start
Starting postgresql95 service:                             [  OK  ]
$ sudo -u postgres createuser -s ec2-user
could not change directory to "/home/ec2-user/environment": Permission denied

Reference: [Rails6] Try running on AWS Cloud9 (Amazon Linux)

Rails new in API mode

$ rails new bbs -d postgresql --api

Add --api to enter Rails API mode. It is created without generating unnecessary files in the API.

Launch a Rails test server

Launch a new terminal from New Terminal.

s6.png

In local etc., the test server can be started only with rails s, but Cloud9 requires an option.

$ cd bbs/
$ rails s -b $IP -p $PORT
...
* Listening on tcp://127.0.0.1:8080
Use Ctrl-C to stop

This will start the server. You can stop it with Ctrl + C as written. You can see that it works on port8080.

Let's go back to the terminal and check with curl if it started up properly. Do not stop the terminal window that started the test server.

$ cd bbs/
$ curl localhost:8080                                                                                                                  
{"status":500,"error":"Internal Server Error","exception":"#\u003cActiveRecord::NoDatabaseError: FATAL:  database \"bbs_development\" does not exist

Apparently I'm angry because I don't have a DB. Let's initialize the DB with rails.

$ rails db:create
...
Created database 'bbs_development'
Created database 'bbs_test'
$ curl localhost:8080                                                                                                                  

If a large amount of html-like character strings appear, it's okay because the 500 series error has not occurred for the time being.

Initialize the IDE

I want the indent to be space2, so go to the setting screen from the gear mark on the right edge of the screen. Select Code Editor (Ace) and

Deflection of Soft Tabs from 4 to 2 On Save, Strip Whitespace

And close.

edit.png

Allow the host

From Rails6, if you do not allow the host, the page will not be displayed with an error. I'm running on Cloud9, so I'll add AWS hosts. After saving, stop the test server once with Ctrl + C and restart it. Otherwise, the config will not be reflected.

config/application.rb


...
module Bbs
  class Application < Rails::Application
...

+    config.hosts << '.amazonaws.com'

...
  end
end

Display the Welcome screen

Press Preview Running Application from the top of the screen to bring up a small application window.

s7.png

However, this small window does not work properly, so press Pop Out Into New Window to bring up another window.

s8.png

Finally, the Welcome screen is displayed normally.

welcome.png

Continued

Building a bulletin board API with authentication authorization in Rails 6 # 2 Introducing git and rubocop [To the serial table of contents]

Recommended Posts

Build a bulletin board API with authentication and authorization with Rails 6 # 1 Environment construction
Build a bulletin board API with authentication authorization with Rails 6 # 2 Introducing git and rubocop
Build a bulletin board API with authentication authorization in Rails # 13 Add authentication header
Build a bulletin board API with authentication and authorization with Rails # 18 ・ Implementation of final user controller
Build a bulletin board API with authentication authorization with Rails 6 # 3 RSpec, FactoryBot introduced and post model
Build a bulletin board API with authentication authorization in Rails # 12 Association of user and post
Build a bulletin board API with authentication authorization in Rails 6 # 5 controller, routes implementation
Introduced # 10 devise_token_auth to build a bulletin board API with authentication authorization in Rails 6
Introducing # 15 pundit to build a bulletin board API with authentication authorization in Rails 6
Build a bulletin board API with authentication authorization in Rails # 17 Add administrator privileges
Build a bulletin board API with authentication authorization in Rails 6 # 11 User model test and validation added
Build a bulletin board API with authentication authorization in Rails 6 # 14 seed Execution time display
# 16 policy setting to build bulletin board API with authentication authorization in Rails 6
# 8 seed implementation to build bulletin board API with authentication authorization in Rails 6
Building a bulletin board API with authentication authorization with Rails 6 Validation and test implementation of # 4 post
Introduced # 9 serializer to build bulletin board API with authentication authorization in Rails 6
# 6 show, create implementation to build bulletin board API with authentication authorization in Rails 6
# 7 update, destroy implementation to build bulletin board API with authentication authorization in Rails 6
Rails6 [API mode] + MySQL5.7 environment construction with Docker
How to build API with GraphQL and Rails
Build Rails (API) x MySQL x Nuxt.js environment with Docker
[Environment construction] Build a Java development environment with VS Code!
[Docker] Rails 5.2 environment construction with docker
[Rails] [Docker] Copy and paste is OK! How to build a Rails development environment with Docker
Build a Node-RED environment with Docker to move and understand
Rails + MySQL environment construction with Docker
Build a Node.js environment with Docker
Build a Tomcat 8.5 environment with Pleiades 4.8
Build a local development environment for Rails tutorials with Docker-Introduce Bootstrap and Font Awesome with Webpack-
Build environment with vue.js + rails + docker
Build Rails environment with Docker Compose
[Environment construction with Docker] Rails 6 & MySQL 8
Steps to build a Ruby on Rails development environment with Vagrant
I tried to make a group function (bulletin board) with Rails
Building Rails 6 and PostgreSQL environment with Docker
Build a PureScript development environment with Docker
Build and manage RStudio environment with Docker-compose
Rails API server environment construction using docker-compose
Environment construction summary with rvm and postgresql
How to build Rails 6 environment with Docker
I built a rails environment with docker and mysql, but I got stuck
Build a Windows application test environment with Selenium Grid, Appium, and Windows Application Driver
[Copy and paste] Build a Laravel development environment with Docker Compose Part 2
[Rails API x Docker] Easy environment construction with shell & operation check with Flutter
How to build a Ruby on Rails development environment with Docker (Rails 6.x)
Build a local development environment for Rails tutorials with Docker (Rails 6 + PostgreSQL + Webpack)
[Copy and paste] Build a Laravel development environment with Docker Compose Participation
Create a SPA with authentication function with Rails API mode + devise_token_auth + Vue.js 3 (Rails edition)
How to build a Ruby on Rails development environment with Docker (Rails 5.x)
Template: Build a Ruby / Rails development environment with a Docker container (Ubuntu version)
Template: Build a Ruby / Rails development environment with a Docker container (Mac version)
Build a development environment for Docker + Rails6 + Postgresql
Build a Laravel / Docker environment with VSCode devcontainer
(Basic authentication) environment variables in rails and Docker
Build a WordPress development environment quickly with Docker
[Win10] Build a JSF development environment with NetBeans
Rails development environment created with VSCode and devcontainer
Prepare a scraping environment with Docker and Java
A reminder of Docker and development environment construction
[Rails] How to build an environment with Docker
Build a Java development environment with VS Code