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.
It is a total of 18 serializations
Without a minimum knowledge of Rails CRUD operations, it can be difficult to catch up.
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
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.
Press Create environment.
Create it with a descriptive name and description. 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.
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
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)
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
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 bbs -d postgresql --api
Add --api to enter Rails API mode. It is created without generating unnecessary files in the API.
Launch a new terminal from New Terminal.
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.
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.
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
Press Preview Running Application from the top of the screen to bring up a small application window.
However, this small window does not work properly, so press Pop Out Into New Window to bring up another window.
Finally, the Welcome screen is displayed normally.
→ Building a bulletin board API with authentication authorization in Rails 6 # 2 Introducing git and rubocop [To the serial table of contents]