Guest User

Untitled

a guest
Jul 13th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. ## post/show (posts_controller.rb) [ruby_on_rails]
  2. def show
  3. @post = Post.find(params[:id])
  4. @comments = @post.comments
  5. @comment = @post.comments.new
  6.  
  7. respond_to do |format|
  8. format.html # show.html.erb
  9. format.xml { render :xml => @post }
  10. end
  11. end
  12.  
  13. ## comment/create (comments_controller.rb) [ruby_on_rails]
  14. def create
  15. @post = Post.find(params[:post_id])
  16. # need to exlude the new comment that existed for the post/show
  17. @comments = @post.comments.reject {|p| p.new_record?}
  18. @comment = @post.comments.build(params[:comment])
  19. if @comment.save
  20. redirect_to @post
  21. else
  22. # need to create an empty views/comments directory so it can go back a level into the posts
  23. render :action => "../posts/show"
  24. end
  25. end
  26.  
  27. ## form_for @comment (posts/show.html.erb) [html_rails]
  28. <% form_for @comment do |f| %>
  29. <p><%= f.error_messages %></p>
  30. <p><%= f.text_area :body %></p>
  31. <p><%= f.submit "Add comment" %></p>
  32. <% end %>
  33.  
  34. ## list current comments (posts/show.html.erb) [html_rails]
  35. <% @comments.each do |comment| %>
  36. <p>
  37. <b>Posted <%= time_ago_in_words(comment.created_at) %> ago</b><br/>
  38. <%= comment.body %>
  39. </p>
  40. <% end %>
Add Comment
Please, Sign In to add comment