Git Product home page Git Product logo

rails-rest-api's Introduction

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 on http://localhost:3000 by default:

$ rails server

Run all RSpec tests and Rubocop:

$ rake test

Usage

HTTP verbs Paths  Used for
POST /register Create a user
POST /login Authenticate a user
GET /posts List all posts
GET /posts/:post_id Show a single post
POST /posts Create a post
PUT /posts/:post_id Update a post
DELETE /posts/:post_id Delete a post
GET /posts/:post_id/comments List all comments of a post
GET /posts/:post_id/comments/:comment_id Show a single comment
POST /posts/:post_id/comments Create a comment
PUT /posts/:post_id/comments/:comment_id Update a comment
DELETE /posts/:post_id/comments/:comment_id Delete a comment

Use Case Examples

Authentication

Create a new user:

$ curl -X POST -H 'Content-type: application/json' -d '{"email": "[email protected]", "password": "testuser123"}' localhost:3000/register

Authenticate a user:

$ curl -X POST -H 'Content-type: application/json' -d '{"email": "[email protected]", "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.

The create, update and delete actions can only be executed by users authorized on admin. A default admin user is definded in db/seeds.rb. After seeding the database, {"email": "[email protected]", "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

The name field is optional with default value anonym.

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

rails-rest-api's People

Contributors

dependabot[bot] avatar sethers avatar yukitas avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

rails-rest-api's Issues

argumentError (wrong number of arguments (given 3, expected 2)):

Processing by AuthenticationController#login as /
Completed 500 Internal Server Error in 3ms (ActiveRecord: 0.0ms)

ArgumentError (wrong number of arguments (given 3, expected 2)):

app/authentications/authenticate_user.rb:4:in initialize' app/controllers/authentication_controller.rb:6:in login'

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.