Guest User

Untitled

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