users_controller.rb 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #---
  2. # Excerpted from "Agile Web Development with Rails",
  3. # published by The Pragmatic Bookshelf.
  4. # Copyrights apply to this code. It may not be used to create training material,
  5. # courses, books, articles, and the like. Contact us if you are in doubt.
  6. # We make no guarantees that this code is fit for any purpose.
  7. # Visit http://www.pragmaticprogrammer.com/titles/rails4 for more book information.
  8. #---
  9. #---
  10. # Excerpted from "Agile Web Development with Rails, 4rd Ed.",
  11. # published by The Pragmatic Bookshelf.
  12. # Copyrights apply to this code. It may not be used to create training material,
  13. # courses, books, articles, and the like. Contact us if you are in doubt.
  14. # We make no guarantees that this code is fit for any purpose.
  15. # Visit http://www.pragmaticprogrammer.com/titles/rails4 for more book information.
  16. #---
  17. class UsersController < ApplicationController
  18. # GET /users
  19. # GET /users.xml
  20. def index
  21. @users = User.order(:name)
  22. respond_to do |format|
  23. format.html # index.html.erb
  24. format.xml { render :xml => @users }
  25. end
  26. end
  27. # GET /users/1
  28. # GET /users/1.xml
  29. def show
  30. @user = User.find(params[:id])
  31. respond_to do |format|
  32. format.html # show.html.erb
  33. format.xml { render :xml => @user }
  34. end
  35. end
  36. # GET /users/new
  37. # GET /users/new.xml
  38. def new
  39. @user = User.new
  40. respond_to do |format|
  41. format.html # new.html.erb
  42. format.xml { render :xml => @user }
  43. end
  44. end
  45. # GET /users/1/edit
  46. def edit
  47. @user = User.find(params[:id])
  48. end
  49. # POST /users
  50. # POST /users.xml
  51. def create
  52. @user = User.new(params[:user])
  53. respond_to do |format|
  54. if @user.save
  55. format.html { redirect_to(users_url,
  56. :notice => "User #{@user.name} was successfully created.") }
  57. format.xml { render :xml => @user,
  58. :status => :created, :location => @user }
  59. else
  60. format.html { render :action => "new" }
  61. format.xml { render :xml => @user.errors,
  62. :status => :unprocessable_entity }
  63. end
  64. end
  65. end
  66. # PUT /users/1
  67. # PUT /users/1.xml
  68. def update
  69. @user = User.find(params[:id])
  70. respond_to do |format|
  71. if @user.update_attributes(params[:user])
  72. format.html { redirect_to(users_url,
  73. :notice => "User #{@user.name} was successfully updated.") }
  74. format.xml { head :ok }
  75. else
  76. format.html { render :action => "edit" }
  77. format.xml { render :xml => @user.errors,
  78. :status => :unprocessable_entity }
  79. end
  80. end
  81. end
  82. # DELETE /users/1
  83. # DELETE /users/1.xml
  84. def destroy
  85. @user = User.find(params[:id])
  86. @user.destroy
  87. respond_to do |format|
  88. format.html { redirect_to(users_url) }
  89. format.xml { head :ok }
  90. end
  91. end
  92. end