[Rails] Comment function (registration / display / deletion)

A memorandum of implementation of the comment function for posts.

goal

--Manage comments in the Comment table (intermediate table) linked to the User table and Post table. --The Coment table has comment contents, commented post_id, and comment poster user_id columns. -(Premise) Use devise for the user function.

Implementation

Comment model and table creation

--Create comment model and table (comment content, comment contributor)

Terminal


% rails g model Comment comment_text:text user:references post:references
% rails g controller Comments
% rails db:migrate

Added association to model.

Comment model


belongs_to :post      # Comment.Get commented posts on post
belongs_to :user      # Comment.Get comment contributor with user

User model


has_many :posts, dependent: :destroy
has_many :comments, dependent: :destroy    # User.Get user comments with comments

validates :comment_text, presence: true, length: { maximum: 1000 }   #Validate the sky, limit the number of characters, etc. ..

Post model


belongs_to :user
has_many :comments, dependent: :destroy    # Post.Get comments for that post with comments

Comment section set up on the detail page

Details page


-#Comment display
= @comments.each do |c|
  = c.user.name
  - if c.user.id == @post.user.id   #Article contributor
    %p Posted by
  = simple_format(c.comment_text)   #Display with line breaks
  = link_to 'Delete', post_comment_path(@post, c), data: {confirm: '本当にDeleteしますか?'}, method: :delete
    -#To delete a comment,@post (parent), @Must be passed to c in comments

-#Post a comment
= form_for [@post, @comment], local: true do |f|   #Since comment is linked to post, write it as an array
  = f.text_area :comment_test
  = f.submit "Post"

Set the routing so that it is linked to the list display page.

routes.rb


resources :posts do   #Nest to link to posts
  resources :comments, only: [:create, :destroy] 
end

Action definition in controller

Comment registration / deletion action (create / destroy).

Terminal


% rails g controller comments

comments controller


before_action :set_post
before_action :authenticate_user!    #Allowed only while logged in

def create
  @comment = @post.comments.create(comment_params)
  if @comment.save
    redirect_to blog_path(@post) notice: 'Commented'
  else
    flash.now[:alert] = 'Comment failed'
    render post_path(@post)
  end
end

def destroy
  @comment = Comment.find(params[:id])
  if @comment.destroy
    redirect_to post_path(@post), notice: 'Deleted comment'
  else
    flash.now[:alert] = 'Failed to delete comment'
    render post_path(@post)
  end
end

private
def set_post
  @post = Post.find(params[:post_id])
end

def comment_params
  params.required(:comment).permit(:comment_text).merge(user_id: current_user.id, post_id: params[:post_id])
end

Since the comment list is also displayed on the post page (show), comment data can be obtained with posts # show.

posts controller


      :
def show
  @post = Post.find(params[:id])

  @comment = Comment.new     #Instantiate for a form(For adding comments)
  @comments = @post.comments #For comment list display
end
      :

Recommended Posts

[Rails] Comment function (registration / display / deletion)
[Rails] Implement credit card registration / deletion function in PAY.JP
[Ruby on Rails] Comment function implementation
[Rails] Comment function implementation procedure memo
[Rails] How to display error messages for comment function (for beginners)
[Rails] Addition of Ruby On Rails comment function
Rails [For beginners] Implementation of comment function
[Ruby on Rails] Logical deletion (withdrawal function)
[Rails 6] Ranking function
Rails follow function
Implemented comment function
[Rails] Notification function
Comment function (Ajax) implementation
[Rails] Implement search function
[Rails] Implemented hashtag function
[Ruby on Rails] Posting score ranking function (whole display)
[rails] tag ranking function
Rails search function implementation
Rails CRUD function implementation ① (new addition, deletion this time)
[Ruby on Rails] Bookmark (favorite registration, like) function: One direction
[Rails] Implement event end function (logical deletion) using paranoia (gem)
Implement user registration function and corporate registration function separately in Rails devise
Implement application function in Rails
Rails fuzzy search function implementation
[Rails] Implement User search function
Introduced graph function with rails
Search function using [rails] ransack
Implement follow function in Rails
[Rails 6] Implementation of search function
[Rails] Implementation of category function
Rails ~ Understanding the message function ~
Display Flash messages in Rails
[Rails] (Supplement) Implemented follow function
Login function implementation with rails
[Rails] EC site cart function
Ajax bookmark function using Rails
About immediate comment update function
[Rails] Implementation of tutorial function
[Rails] Implement image posting function
[Rails] Implementation of like function
App creation comment function asynchronous
[Rails 6] Pagination function implementation (kaminari)
[Rails] Implementation of new registration function in wizard format using devise
Ruby on Rails for beginners! !! Post list / detailed display function summary
[Rails 6] Implementation of new registration function by SNS authentication (Facebook, Google)
[Rails] Implementation of coupon function (with automatic deletion function using batch processing)