users_controller.rb 949 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. class UsersController < ApplicationController
  2. before_action :set_user, only: [:show, :edit, :update, :destroy]
  3. before_action :correct_user, only: [:edit, :update]
  4. def show
  5. end
  6. def new
  7. end
  8. def edit
  9. end
  10. def update
  11. if @user.update(user_params)
  12. flash[:success] = 'Profile updated'
  13. redirect_to @user
  14. else
  15. render :edit
  16. end
  17. end
  18. private
  19. # Use callbacks to share common setup or constraints between actions.
  20. def set_user
  21. @user = User.find(params[:id])
  22. end
  23. # Only allow certain attributes to be updated over the web.
  24. def user_params
  25. params.require(:user).permit(:login, :email, :password,
  26. :password_confirmation,
  27. :gender, :phone)
  28. end
  29. # Confirms the correct user.
  30. def correct_user
  31. @user = User.find(params[:id])
  32. redirect_to(root_url) unless current_user?(@user)
  33. end
  34. end