Class: Api::UsersController

Inherits:
ApplicationController
  • Object
show all
Includes:
ActionController::Serialization, LoggedInControllerConcern
Defined in:
app/controllers/api/users_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject

Creates a new user



10
11
12
13
14
15
16
17
# File 'app/controllers/api/users_controller.rb', line 10

def create
  @user = User.new(params.permit(:name, :email, :password, :password_confirmation))
  if @user.save
    render json: @user, status: :created
  else
    render json: @user.errors.full_messages, status: 422
  end
end

#searchObject

Returns the user with matching email

‘GET /api/users/search?email=xxx`



31
32
33
34
35
36
37
38
# File 'app/controllers/api/users_controller.rb', line 31

def search
  user = User.where("email = ? AND id != ?", params[:email], @user.id).first
  if user
    render json: user
  else
    render json: {}, status: :not_found
  end
end

#showObject

Renders the current user as json

‘GET /api/user/:id`



23
24
25
# File 'app/controllers/api/users_controller.rb', line 23

def show
  render json: @user
end