Class: UsersController

Inherits:
ApplicationController show all
Defined in:
app/controllers/users_controller.rb

Overview

This provides addional actions for users in addition to [Devise::Registrations] for example adding and removing roles from users.

Instance Method Summary collapse

Methods inherited from ApplicationController

#honeypot

Instance Method Details

#destroyObject

DESTROY /users/:id



23
24
25
26
27
28
29
30
# File 'app/controllers/users_controller.rb', line 23

def destroy
  if @user == current_user
    return redirect_to users_path, flash: { alert: "You cannot delete your own accout!" }
  end

  @user.destroy!
  redirect_to users_path, flash: { success: "User deleted." }
end

#editObject

GET /users/:id/edit



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

def edit
  Role::AVAILABLE_ROLES.each do |role|
    # This adds an unsaved role to the user if it does not exist
    @user.roles.build(name: role) unless @user.has_role?(role)
  end
end

#indexObject

GET /users/



17
18
19
20
# File 'app/controllers/users_controller.rb', line 17

def index
  @title = "Users"
  @users = User.all
end

#showObject

GET /users/:id



9
10
11
12
13
14
# File 'app/controllers/users_controller.rb', line 9

def show
  @title = @user.nickname
  @available_roles =  Role.all.to_a.keep_if do |role|
    !@user.has_role?(role.name.to_sym)
  end
end

#updateObject

PATCH /users/:id



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

def update
  if @user.update(update_params)
    redirect_to @user, notice: 'User updated.'
  else
    render :edit
  end
end