Class: KnockOnce::UsersController

Inherits:
ApplicationController show all
Includes:
ActiveModel::SecurePassword
Defined in:
app/controllers/knock_once/users_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



42
43
44
45
46
47
48
49
# File 'app/controllers/knock_once/users_controller.rb', line 42

def create
  @user = User.new(user_params)
  if @user.save
    render json: @user
  else
    render json: @user.errors.full_messages, status: :unprocessable_entity
  end
end

#destroyObject



29
30
31
32
33
34
35
36
37
38
39
40
# File 'app/controllers/knock_once/users_controller.rb', line 29

def destroy
  @user = current_user
  if @user.authenticate(params[:current_password])
    if @user.destroy
      render json: :success
    else
      render json: @user.errors.full_messages
    end
  else
    render status: :unprocessable_entity, json: ['Current password is incorrect']
  end
end

#showObject



8
9
10
11
# File 'app/controllers/knock_once/users_controller.rb', line 8

def show
  @user = current_user
  render json: @user
end

#updateObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'app/controllers/knock_once/users_controller.rb', line 13

def update
  @user = User.find_by_id(current_user.id)
  if @user.authenticate(params[:current_password])
    if @user.update(user_params)
      render json: {
        user: @user,
        message: 'Your profile has been updated!'
      }
    else
      render json: @user.errors.full_messages, status: :unprocessable_entity
    end
  else
    render status: :unprocessable_entity, json: ['Current password is incorrect']
  end
end