0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

RailsAdvent Calendar 2020

Day 25

[Rails]超簡単なRailsAPIを作ってみる

Last updated at Posted at 2020-12-24

##はじめに
railsAPIを作ってみたので備忘録として残す。
次回はswaggerなどのAPIドキュメントの導入をやってみる

##Rials APIの作り方
rails newに--apiを付ける

rails new rails-test-api --api

##簡単な使い方
CRUDを生成する
今回は掲示板CRUDを作成

rails generate scaffold Board title:string text:string
rails db:create
rails db:migrate

アプリケーションを起動

rails s

http://localhost:3000/boards に接続

[]が表示されるCurlでPOSTを叩く

curl -X POST -H "Content-Type: application/json" -d '{"title": "test", "text": "テストです"}' http://localhost:3000/boards

※Curlの叩き方はこちら

再度 http://localhost:3000/boards に接続するとデータが作成される

[{"id":1,"title":"test","text":"テストです","created_at":"2020-12-24T17:27:15.312Z","updated_at":"2020-12-24T17:27:15.312Z"}]

これで完成

##参考
https://railsguides.jp/api_app.html
https://qiita.com/hanata-el/items/d96d84d1889b495aca00

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?