Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Post < ApplicationRecord
- has_many :comments, dependent: :destroy
- end
- class Comment < ApplicationRecord
- belongs_to :post
- validates :comment_content, presence: true, length: { maximum: 5000 }
- end
- class CommentsController < ApplicationController
- def create
- @post = Post.find(params[:post_id])
- @comment = @post.comments.create(params[:comment].permit(:comment_content))
- redirect_to post_path(@post)
- end
- end
- <%= simple_form_for([@post, @post.comments.build]) do |f| %>
- <%= f.input :comment_content, input_html: { class: 'texture' }, wrapper: false, label_html: { class: 'label'
- } %>
- <%= f.button :submit, 'Leave a reply', class: "btn btn-primary" %>
- <% end %>
- <%= comment.comment_content %>
- <p id="notice"><%= notice %></p>
- <p>
- <!-- Post details -->
- </p>
- <%= render @post.comments %>
- <%= render 'comments/form' %>
- Rails.application.routes.draw do
- resources :posts do
- resources :comments
- end
- end
- <%= f.error_notification %>
- <%= f.error_notification message: f.object.errors[:base].to_sentence if f.object.errors[:base].present? %>
- def create
- @post = Post.find(params[:post_id])
- @comment = @post.comments.build(params[:comment].permit(:comment_content))
- if @comment.save
- redirect_to post_path(@post), notice: 'Comment was successfully created.'
- else
- render 'posts/show'
- end
- end
Add Comment
Please, Sign In to add comment