Class: Guts::UsersController

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

Overview

Users controller

Instance Method Summary collapse

Methods included from SessionsHelper

#current_user, #log_in, #log_out, #logged_in?

Instance Method Details

#createObject

Note:

Redirects to #index if successfull or re-renders #new if not

Creates a user through post



34
35
36
37
38
39
40
41
42
# File 'app/controllers/guts/users_controller.rb', line 34

def create
  @user = User.new user_params

  if @user.save
    redirect_to users_path, notice: "User was successfully created."
  else
    render :new
  end
end

#destroyObject

Note:

Redirects to #index on success

Destroys a single user



56
57
58
59
# File 'app/controllers/guts/users_controller.rb', line 56

def destroy
  @user.destroy
  redirect_to users_url, notice: "User was successfully destroyed."
end

#editObject

Editing of a user



29
30
# File 'app/controllers/guts/users_controller.rb', line 29

def edit
end

#indexObject

Note:

Filterable by group by passing ‘group` param

Displays a list of users



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

def index
  if params[:group]
    @group = Group.find params[:group]
    @users = User.includes(:groups).where(guts_groups: {id: @group.id})
  else
    @users = User.all
  end
end

#newObject

Creation of a new user



24
25
26
# File 'app/controllers/guts/users_controller.rb', line 24

def new
  @user = User.new
end

#showObject

Show details about a single user



20
21
# File 'app/controllers/guts/users_controller.rb', line 20

def show
end

#switch_userObject

Allows switching of users by passing ‘user_id` in params



63
64
65
66
67
68
69
70
71
# File 'app/controllers/guts/users_controller.rb', line 63

def switch_user
  if request.post?
    user = User.find(params[:user_id])
     user
    flash.now[:notice] = "You are now logged in as #{user.name}."
  end
  
  @users = User.all
end

#updateObject

Note:

Redirects to #index if successfull or re-renders #edit if not

Updates a user through patch



46
47
48
49
50
51
52
# File 'app/controllers/guts/users_controller.rb', line 46

def update
  if @user.update(user_params)
    redirect_to users_path, notice: "User was successfully updated."
  else
    render :edit
  end
end