Guest User

Untitled

a guest
Aug 25th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. class Post < ApplicationRecord
  2. has_many :comments, dependent: :destroy
  3. end
  4.  
  5. class Comment < ApplicationRecord
  6. belongs_to :post
  7. validates :comment_content, presence: true, length: { maximum: 5000 }
  8. end
  9.  
  10. class CommentsController < ApplicationController
  11. def create
  12. @post = Post.find(params[:post_id])
  13. @comment = @post.comments.create(params[:comment].permit(:comment_content))
  14. redirect_to post_path(@post)
  15. end
  16. end
  17.  
  18. <%= simple_form_for([@post, @post.comments.build]) do |f| %>
  19. <%= f.input :comment_content, input_html: { class: 'texture' }, wrapper: false, label_html: { class: 'label'
  20. } %>
  21.  
  22. <%= f.button :submit, 'Leave a reply', class: "btn btn-primary" %>
  23. <% end %>
  24.  
  25. <%= comment.comment_content %>
  26.  
  27. <p id="notice"><%= notice %></p>
  28.  
  29. <p>
  30. <!-- Post details -->
  31. </p>
  32.  
  33. <%= render @post.comments %>
  34.  
  35. <%= render 'comments/form' %>
  36.  
  37. Rails.application.routes.draw do
  38. resources :posts do
  39. resources :comments
  40. end
  41. end
  42.  
  43. <%= f.error_notification %>
  44. <%= f.error_notification message: f.object.errors[:base].to_sentence if f.object.errors[:base].present? %>
  45.  
  46. def create
  47. @post = Post.find(params[:post_id])
  48. @comment = @post.comments.build(params[:comment].permit(:comment_content))
  49. if @comment.save
  50. redirect_to post_path(@post), notice: 'Comment was successfully created.'
  51. else
  52. render 'posts/show'
  53. end
  54. end
Add Comment
Please, Sign In to add comment