2024-12-31 14:20:22 +08:00
|
|
|
class UsersController < ApplicationController
|
2025-01-02 11:59:27 +08:00
|
|
|
include SessionsHelper
|
2025-01-05 18:27:13 +08:00
|
|
|
before_action :logged_in_user, only: [ :index, :edit, :update, :destroy ]
|
2025-01-04 10:21:22 +08:00
|
|
|
before_action :correct_user, only: [ :edit, :update ]
|
2025-01-05 18:27:13 +08:00
|
|
|
before_action :admin_user, only: [ :destroy ]
|
2025-01-05 01:59:05 +08:00
|
|
|
|
|
|
|
def index
|
2025-01-05 17:50:33 +08:00
|
|
|
# @users = User.all
|
|
|
|
# @users = User.order(:name).page(params[:page])
|
2025-01-06 18:38:39 +08:00
|
|
|
@users = User.where(activated: true).page(params[:page])
|
2025-01-05 01:59:05 +08:00
|
|
|
end
|
|
|
|
|
2024-12-31 16:34:52 +08:00
|
|
|
def show
|
|
|
|
@user = User.find(params[:id])
|
2025-01-06 18:38:39 +08:00
|
|
|
redirect_to root_url and return unless @user.activated?
|
2024-12-31 16:34:52 +08:00
|
|
|
# debugger
|
|
|
|
end
|
2024-12-31 14:20:22 +08:00
|
|
|
def new
|
2024-12-31 16:34:52 +08:00
|
|
|
@user = User.new
|
|
|
|
# debugger
|
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
|
|
|
@user = User.new(user_params)
|
|
|
|
if @user.save
|
2025-01-06 18:38:39 +08:00
|
|
|
# reset_session
|
|
|
|
# log_in @user
|
|
|
|
# flash[:success] = "Welcome to the Sample App!"
|
|
|
|
# redirect_to @user
|
2024-12-31 16:34:52 +08:00
|
|
|
# redirect_to user_url(@user)
|
2025-01-08 10:14:36 +08:00
|
|
|
@user.send_activation_email
|
2025-01-06 18:38:39 +08:00
|
|
|
flash[:info] = "Please check your email to activate your account."
|
|
|
|
redirect_to root_url
|
2024-12-31 16:34:52 +08:00
|
|
|
else
|
2025-01-08 10:14:36 +08:00
|
|
|
render "new", status: :unprocessable_entity
|
2024-12-31 16:34:52 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2025-01-03 10:55:42 +08:00
|
|
|
def edit
|
|
|
|
@user = User.find(params[:id])
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
|
|
|
@user = User.find(params[:id])
|
|
|
|
if @user.update(user_params)
|
|
|
|
flash[:success] = "Profile updated"
|
|
|
|
redirect_to @user
|
|
|
|
# redirect_to user_url(@user)
|
|
|
|
else
|
2025-01-04 10:21:22 +08:00
|
|
|
render "edit"
|
2025-01-03 10:55:42 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2025-01-05 18:27:13 +08:00
|
|
|
def destroy
|
|
|
|
User.find(params[:id]).destroy
|
|
|
|
flash[:success] = "User deleted"
|
|
|
|
redirect_to users_url
|
|
|
|
end
|
|
|
|
|
2024-12-31 16:34:52 +08:00
|
|
|
private
|
|
|
|
|
|
|
|
def user_params
|
|
|
|
params.require(:user).permit(:name, :email, :password,
|
|
|
|
:password_confirmation)
|
2024-12-31 14:20:22 +08:00
|
|
|
end
|
2025-01-03 11:12:51 +08:00
|
|
|
|
|
|
|
def logged_in_user
|
|
|
|
unless logged_in?
|
2025-01-03 13:48:59 +08:00
|
|
|
store_location
|
2025-01-03 11:12:51 +08:00
|
|
|
flash[:danger] = "Please log in."
|
|
|
|
redirect_to login_url
|
|
|
|
end
|
|
|
|
end
|
2025-01-03 13:48:59 +08:00
|
|
|
|
|
|
|
def correct_user
|
|
|
|
@user = User.find(params[:id])
|
|
|
|
redirect_to(root_url) unless current_user?(@user)
|
|
|
|
end
|
2025-01-05 18:27:13 +08:00
|
|
|
|
|
|
|
def admin_user
|
|
|
|
redirect_to(root_url) unless current_user.admin?
|
|
|
|
end
|
2024-12-31 14:20:22 +08:00
|
|
|
end
|