Guest User

Untitled

a guest
Mar 12th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.46 KB | None | 0 0
  1. class CommentsController < ApplicationController
  2. before_filter :load_post
  3. # GET /comments
  4. # GET /comments.xml
  5. def index
  6. @comments = @post.comments.find(:all)
  7.  
  8. respond_to do |format|
  9. format.html # index.html.erb
  10. format.xml { render :xml => @comments }
  11. end
  12. end
  13.  
  14. # GET /comments/1
  15. # GET /comments/1.xml
  16. def show
  17. @comment = @post.comments.find(params[:id])
  18.  
  19. respond_to do |format|
  20. format.html # show.html.erb
  21. format.xml { render :xml => @comment }
  22. end
  23. end
  24.  
  25. # GET /comments/new
  26. # GET /comments/new.xml
  27. def new
  28. @comment = @post.comments.build
  29.  
  30. respond_to do |format|
  31. format.html # new.html.erb
  32. format.xml { render :xml => @comment }
  33. end
  34. end
  35.  
  36. # GET /comments/1/edit
  37. def edit
  38. @comment = @post.comments.find(params[:id])
  39. end
  40.  
  41. # POST /comments
  42. # POST /comments.xml
  43. def create
  44. @comment = @post.comments.build(params[:comment])
  45.  
  46. respond_to do |format|
  47. if @comment.save
  48. # Moje testowanie, co i gdzie sie da :)
  49. if logged_in?
  50. flash[:notice] = 'Komentarz dodany, gratulujemy. Dodałeś go jako zalogowaniec'
  51. else
  52. flash[:notice] = 'Komentarz dodany, gratulujemy. Dodałeś go jako niezalogowaniec.'
  53. end
  54. format.html { redirect_to([@post]) }
  55. format.xml { render :xml => @comment, :status => :created, :location => @comment }
  56. else
  57. format.html { render :action => "new" }
  58. format.xml { render :xml => @comment.errors, :status => :unprocessable_entity }
  59. end
  60. end
  61. end
  62.  
  63. # PUT /comments/1
  64. # PUT /comments/1.xml
  65. def update
  66. @comment = @post.comments.find(params[:id])
  67.  
  68. respond_to do |format|
  69. if @comment.update_attributes(params[:comment])
  70. flash[:notice] = 'Comment was successfully updated.'
  71. format.html { redirect_to([@post, @comment]) }
  72. format.xml { head :ok }
  73. else
  74. format.html { render :action => "edit" }
  75. format.xml { render :xml => @comment.errors, :status => :unprocessable_entity }
  76. end
  77. end
  78. end
  79.  
  80. # DELETE /comments/1
  81. # DELETE /comments/1.xml
  82. def destroy
  83. @comment = @post.comments.find(params[:id])
  84. @comment.destroy
  85.  
  86. respond_to do |format|
  87. format.html { redirect_to(post_comments_url(@post)) }
  88. format.xml { head :ok }
  89. end
  90. end
  91.  
  92. def load_post
  93. @post = Post.find(params[:post_id])
  94. end
  95.  
  96. end
Add Comment
Please, Sign In to add comment