Guest User

Untitled

a guest
Apr 16th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.10 KB | None | 0 0
  1. <h1>
  2. <%= @post.title %>
  3. </h1>
  4.  
  5. <p>
  6. <%= @post.body %>
  7. </p>
  8. <h1 class="comments">Comments</h1><br />
  9. <!-- #1 -->
  10. <% unless @post.comments.empty? %>
  11. <% @post.comments.each do |comment| %>
  12. <p><%= h comment.body %><br /><small>Posted on <%= h comment.created_at %> by <%= comment.user_id.login %></small>
  13. </p>
  14. <% end %>
  15. <% end %>
  16.  
  17. <!-- #2 -->
  18. <h2 class="comments">Add a new Comment</h2><br />
  19. <% if logged_in? %>
  20. <%= render :partial => @comment = Comment.new,
  21. :locals => { :button_name => 'Create'}%>
  22. <% else %>
  23. <p> Please <%= link_to "Login", login_path %> or <%= link_to "Sign Up", signup_path %> to enter a comment. Thank you</p>
  24. <% end %>
  25. <%= link_to 'Back', posts_path %>
  26. <br /><%= debug(session) %>
  27.  
  28. ----
  29.  
  30. controller
  31.  
  32. class CommentsController < ApplicationController
  33. before_filter :load_post
  34. # GET /comments
  35. # GET /comments.xml
  36. def index
  37. @comments = @post.comments.find(:all)
  38.  
  39. respond_to do |format|
  40. format.html # index.html.erb
  41. format.xml { render :xml => @comments }
  42. end
  43. end
  44.  
  45. # GET /comments/1
  46. # GET /comments/1.xml
  47. def show
  48. @comment = @post.comments.find(params[:id])
  49. @comment.user_id = @current_user
  50. respond_to do |format|
  51. format.html # show.html.erb
  52. format.xml { render :xml => @comment }
  53. end
  54. end
  55.  
  56. # GET /comments/new
  57. # GET /comments/new.xml
  58. def new
  59. @comment = @post.comments.build
  60. @post.comment.user_id = @current_user
  61. respond_to do |format|
  62. format.html # new.html.erb
  63. format.xml { render :xml => @comment }
  64. end
  65. end
  66.  
  67. # GET /comments/1/edit
  68. def edit
  69. @comment = @post.comments.find(params[:id])
  70. end
  71.  
  72. # POST /comments
  73. # POST /comments.xml
  74. def create
  75. @comment = @post.comments.build(params[:comment])
  76. @comment.user_id = @current_user
  77. respond_to do |format|
  78. if @comment.save
  79. flash[:notice] = 'Comment was successfully created.'
  80. format.html { redirect_to(@post) }
  81. format.xml { render :xml => @comment, :status => :created, :location => @comment }
  82. else
  83. format.html { render :action => "new" }
  84. format.xml { render :xml => @comment.errors, :status => :unprocessable_entity }
  85. end
  86. end
  87. end
  88.  
  89. # PUT /comments/1
  90. # PUT /comments/1.xml
  91. def update
  92. @comment = @post.comments.find(params[:id])
  93.  
  94. respond_to do |format|
  95. if @comment.update_attributes(params[:comment])
  96. flash[:notice] = 'Comment was successfully updated.'
  97. format.html { redirect_to(@post) }
  98. format.xml { head :ok }
  99. else
  100. format.html { render :action => "edit" }
  101. format.xml { render :xml => @comment.errors, :status => :unprocessable_entity }
  102. end
  103. end
  104. end
  105.  
  106. # DELETE /comments/1
  107. # DELETE /comments/1.xml
  108. def destroy
  109. @comment = @post.comments.find(params[:id])
  110. @comment.destroy
  111.  
  112. respond_to do |format|
  113. format.html { redirect_to(post_comments_url) }
  114. format.xml { head :ok }
  115. end
  116. end
  117. def load_post
  118. @post = Post.find(params[:post_id])
  119. end
  120. end
Add Comment
Please, Sign In to add comment