microposts_controller.rb 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. class MicropostsController < ApplicationController
  2. # GET /microposts
  3. # GET /microposts.json
  4. def index
  5. @microposts = Micropost.all
  6. respond_to do |format|
  7. format.html # index.html.erb
  8. format.json { render json: @microposts }
  9. end
  10. end
  11. # GET /microposts/1
  12. # GET /microposts/1.json
  13. def show
  14. @micropost = Micropost.find(params[:id])
  15. respond_to do |format|
  16. format.html # show.html.erb
  17. format.json { render json: @micropost }
  18. end
  19. end
  20. # GET /microposts/new
  21. # GET /microposts/new.json
  22. def new
  23. @micropost = Micropost.new
  24. respond_to do |format|
  25. format.html # new.html.erb
  26. format.json { render json: @micropost }
  27. end
  28. end
  29. # GET /microposts/1/edit
  30. def edit
  31. @micropost = Micropost.find(params[:id])
  32. end
  33. # POST /microposts
  34. # POST /microposts.json
  35. def create
  36. @micropost = Micropost.new(params[:micropost])
  37. respond_to do |format|
  38. if @micropost.save
  39. format.html { redirect_to @micropost, notice: 'Micropost was successfully created.' }
  40. format.json { render json: @micropost, status: :created, location: @micropost }
  41. else
  42. format.html { render action: "new" }
  43. format.json { render json: @micropost.errors, status: :unprocessable_entity }
  44. end
  45. end
  46. end
  47. # PUT /microposts/1
  48. # PUT /microposts/1.json
  49. def update
  50. @micropost = Micropost.find(params[:id])
  51. respond_to do |format|
  52. if @micropost.update_attributes(params[:micropost])
  53. format.html { redirect_to @micropost, notice: 'Micropost was successfully updated.' }
  54. format.json { head :ok }
  55. else
  56. format.html { render action: "edit" }
  57. format.json { render json: @micropost.errors, status: :unprocessable_entity }
  58. end
  59. end
  60. end
  61. # DELETE /microposts/1
  62. # DELETE /microposts/1.json
  63. def destroy
  64. @micropost = Micropost.find(params[:id])
  65. @micropost.destroy
  66. respond_to do |format|
  67. format.html { redirect_to microposts_url }
  68. format.json { head :ok }
  69. end
  70. end
  71. end