users_controller.rb 1.0 KB

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