Ruby on Rails で API を作成

こちらと同じことを行いました。
[Rails で「API を作る」を体感しよう](https://zenn.dev/mochinozenn/articles/190e2e8b8f1edb)

## 確認したバージョン

```text
$ ruby -v
ruby 3.2.2 (2023-03-30 revision e51014f9c0) [x86_64-linux]

$ rails -v
Rails 7.0.7.2
```

## プロジェクトの作成

```bash
rails new sample-api --api
```

実行結果

```text
$ rails new sample-api --api
（省略）
      remove  config/initializers/permissions_policy.rb
      remove  config/initializers/new_framework_defaults_7_0.rb
         run  bundle install
Fetching gem metadata from https://rubygems.org/...........
Resolving dependencies...
Bundle complete! 6 Gemfile dependencies, 56 gems now installed.
Use `bundle info [gemname]` to see where a bundled gem is installed.
         run  bundle binstubs bundler
```

## サーバーの起動

```bash
rails s -b 0.0.0.0
```

ブラウザーでアクセス
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/179446/6069565c-5f2a-188f-68b0-c7f817ea2d1f.png)

## model の作成

```bash
rails g model User name:string age:integer hobby:string
```

実行結果

```text
$ rails g model User name:string age:integer hobby:string
      invoke  active_record
      create    db/migrate/20230903042637_create_users.rb
      create    app/models/user.rb
      invoke    test_unit
      create      test/models/user_test.rb
      create      test/fixtures/users.yml
```

```bash
rails db:migrate
```

実行結果

```text
$ rails db:migrate
== 20230903042637 CreateUsers: migrating ======================================
-- create_table(:users)
   -> 0.0016s
== 20230903042637 CreateUsers: migrated (0.0017s) =============================
```

## 　データを入れ込む
db/seeds.rb を次で置き換え

```rb:db/seeds.rb
users = [
  ["たけし", 20, "サッカー"],
  ["さおり", 23, "バレー"],
  ["たろう", 22, "野球"],
  ["ひろみ", 22, "テニス"]
]

users.each do |name, age, hobby| 
  User.create(name: name, age: age, hobby: hobby)
end
```

```bash
rails db:seed
```

## データの確認


>irb(main):001>
>というプロンプトに対して、
>User.all
>と入力します。

```text
$ rails c --sandbox
Loading development environment in sandbox (Rails 7.0.7.2)
Any modifications you make will be rolled back on exit
irb(main):001> User.all
  TRANSACTION (0.1ms)  begin transaction
  User Load (0.2ms)  SELECT "users".* FROM "users"
=> 
[#<User:0x00007f75c1b85f18
  id: 1,
  name: "たけし",
  age: 20,
  hobby: "サッカー",
  created_at: Sun, 03 Sep 2023 04:31:01.324002000 UTC +00:00,
  updated_at: Sun, 03 Sep 2023 04:31:01.324002000 UTC +00:00>,
 #<User:0x00007f75c19199c8
  id: 2,
  name: "さおり",
  age: 23,
  hobby: "バレー",
  created_at: Sun, 03 Sep 2023 04:31:01.340040000 UTC +00:00,
  updated_at: Sun, 03 Sep 2023 04:31:01.340040000 UTC +00:00>,
 #<User:0x00007f75c1919928
  id: 3,
  name: "たろう",
  age: 22,
  hobby: "野球",
  created_at: Sun, 03 Sep 2023 04:31:01.354874000 UTC +00:00,
  updated_at: Sun, 03 Sep 2023 04:31:01.354874000 UTC +00:00>,
 #<User:0x00007f75c1919888
  id: 4,
  name: "ひろみ",
  age: 22,
  hobby: "テニス",
  created_at: Sun, 03 Sep 2023 04:31:01.369581000 UTC +00:00,
  updated_at: Sun, 03 Sep 2023 04:31:01.369581000 UTC +00:00>]
irb(main):002> exit
```

sqlite3 コマンドで確認

```text
$ sqlite3 storage/development.sqlite3
SQLite version 3.42.0 2023-05-16 12:36:15
Enter ".help" for usage hints.
sqlite> .tables
ar_internal_metadata  schema_migrations     users               
sqlite> select * from users;
1|たけし|20|サッカー|2024-02-21 02:48:21.190020|2024-02-21 02:48:21.190020
2|さおり|23|バレー|2024-02-21 02:48:21.193850|2024-02-21 02:48:21.193850
3|たろう|22|野球|2024-02-21 02:48:21.196261|2024-02-21 02:48:21.196261
4|ひろみ|22|テニス|2024-02-21 02:48:21.211052|2024-02-21 02:48:21.211052
sqlite> .q
```

## ルーティングの設定
config/routes.rb を次で置き換え

```rb:config/routes.rb
Rails.application.routes.draw do
  resources :users, only: :index
end
```

## コントローラーの作成

```bash
rails g controller Users
```

実行結果

```text
$ rails g controller Users
      create  app/controllers/users_controller.rb
      invoke  test_unit
      create    test/controllers/users_controller_test.rb
```

app/controllers/users_controller.rb を次で置き換え

```rb:app/controllers/users_controller.rb
class UsersController < ApplicationController
def index
 users = User.all
 render status: 200, json: { users: users }
end
end
```

## サーバーの起動

```bash
rails s -b 0.0.0.0
```

Httpie でアクセス

```bash
http http://127.0.0.1:3000/users/
```

curl でアクセス

```bash
curl http://127.0.0.1:3000/users/ | jq .
```