Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
/rails-rest-apiPublic template

A simple RoR 6 REST API demo with JWT authentication.

License

NotificationsYou must be signed in to change notification settings

YuKitAs/rails-rest-api

Repository files navigation

example workflow

Rails Rest API

Project Setup

Install all gems:

$bundle install

Update the database with new data model:

$rake db:migrate

Feed the database with default seeds:

$rake db:seed

Start the web server onhttp://localhost:3000 by default:

$rails server

Run all RSpec tests and Rubocop:

$raketest

Usage

HTTP verbsPaths Used for
POST/registerCreate a user
POST/loginAuthenticate a user
GET/postsList all posts
GET/posts/:post_idShow a single post
POST/postsCreate a post
PUT/posts/:post_idUpdate a post
DELETE/posts/:post_idDelete a post
GET/posts/:post_id/commentsList all comments of a post
GET/posts/:post_id/comments/:comment_idShow a single comment
POST/posts/:post_id/commentsCreate a comment
PUT/posts/:post_id/comments/:comment_idUpdate a comment
DELETE/posts/:post_id/comments/:comment_idDelete a comment

Use Case Examples

Authentication

Create a new user:

$curl -X POST -H'Content-type: application/json' -d'{"email": "testuser@email.com", "password": "testuser123"}' localhost:3000/register

Authenticate a user:

$curl -X POST -H'Content-type: application/json' -d'{"email": "testuser@email.com", "password": "testuser123"}' localhost:3000/login

On successful login,{"auth_token": <token>} will be returned. This token will be expired after 24 hours.

CRUD

In order to access the posts and comments, add-H 'Authorization: <token>' to the header of every request for CRUD operations.

Thecreate,update anddelete actions can only be executed by users authorized on admin. A default admin user is definded indb/seeds.rb. After seeding the database,{"email": "admin@email.com", "password": "admin123"} can be used to login as an admin.

Create a new post:

$curl -X POST -H'Content-type: application/json' -d'{"title": "My title", "content": "My content"}' localhost:3000/posts

Create a new comment:

$curl -X POST -H'Content-type: application/json' -d'{"name": "YuKitAs", "message": "My message"}' localhost:3000/posts/1/comments

Thename field is optional with default valueanonym.

Update an existing post by id:

$curl -X PUT -H'Content-type: application/json' -d'{"title": "My new title", "content": "My new content"}' localhost:3000/posts/1

Update an existing comment by id:

$curl -X PUT -H'Content-type: application/json' -d'{"name": "YuKitAs", "message": "My new message"}' localhost:3000/posts/2/comments/1

Delete an existing post by id:

$curl -X DELETE localhost:3000/posts/1

All the comments of this post will be deleted as well.

Delete an existing comment by id:

$curl -X DELETE localhost:3000/posts/2/comments/1

[8]ページ先頭

©2009-2025 Movatter.jp