Skip to content

Instantly share code, notes, and snippets.

@vanstee
Last active August 29, 2015 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vanstee/271944385a0d9cb2e50b to your computer and use it in GitHub Desktop.
Save vanstee/271944385a0d9cb2e50b to your computer and use it in GitHub Desktop.

Example singular request

curl -H "application/json" https://example.com/users/174835f6-cb96-4806-854e-c0eb6ae72d23

{
  "user": {
    "name": "Patrick Van Stee",
    "email": "patrick@vanstee.me"
  }
}

Example plural request

curl -H "application/json" https://example.com/users

{
  "users": [
    {
      "name": "Patrick Van Stee",
      "email": "patrick@vanstee.me"
    },
    {
      "name": "Wesley Lancel",
      "email": "something@idontremember.com"
    }
  ]
}
Rails.application.routes.draw do
defaults format: :json do
use_doorkeeper scope: '/'
resource :current_user,
path: '/user',
only: [:show]
resources :projects,
only: [:index, :create, :show, :update]
resources :users,
only: [:create, :show, :update]
end
end
class User < ActiveRecord::Base
has_many :projects
has_secure_password
def self.authenticate(email, password)
user = User.find_by(email: email)
user && user.authenticate(password)
end
end
class UserSerializer < ApplicationSerializer
attributes :email, :name
has_many :projects,
serializer: ProjectSerializer
end
class UsersController < ApplicationController
doorkeeper_for :show, :update
respond_to :json
skip_after_action :verify_authorized,
only: :create
def create
user = User.create(user_params)
respond_with user
end
def show
user = User.find(params[:id])
authorize user
respond_with user
end
def update
user = User.find(params[:id])
authorize user
user.update(user_params)
respond_with user
end
def user_params
params.require(:user).
permit(:email, :name, :password)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment