Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

controller_users

View gist:8364397
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
class UsersController < ApplicationController
before_action :set_user, only: [:show, :edit, :update]
before_action :require_same_user, only: [:edit, :update]
 
 
def index
@user = User.all
end
 
def show
@user = User.find(params[:id])
end
 
def new
@user = User.new
end
 
def create
@user = User.new(user_params)
 
if @user.save
flash[:notice] = "You are registered."
redirect_to root_path
else
render :new
end
end
 
def edit
@user = User.find(params[:id])
end
 
def update
if @user.update(user_params)
flash[:notice] = "profile updated"
redirect_to root_path
else
render :edit
end
 
end
 
def write_private_message
@private_message = PrivateMessage.new
@user = User.find(params[:id])
end
 
def send_private_message
@private_message = PrivateMessage.new(private_message_params)
@notification = Notification.create(notificationable_type: @private_message, user: current_user, notification: params[:notification])
redirect_to user_path
end
 
private
 
def private_message_params
params.require(:private_message).permit(:sender_uid, :receiver_uid, :subject, :body)
end
 
def user_params
params.require(:user).permit(:first_name, :last_name, :password)
end
 
def set_user
@user = User.find(params[:id])
end
 
def require_same_user
if current_user != @user
flash[:error] = "You're not allowed to do that"
redirect_to root_path
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.