Rails [For beginners] Implementation of comment function

I summarized the implementation of the comment function in rails.

Premise

The development environment for Ruby on Rails is in place. Posts (post table) and User (user table) have already been created. The User table uses gem devise. This time we will implement a comment for the post.

Implementation of comment function

1. Creating a Comment model

The details and relations of the Comments table to be created are as follows. スクリーンショット 2020-11-11 6.28.55.png

Modeling in the terminal

$ rails g model comment

Description of migration file

20********_create_comments.rb


class CreateComments < ActiveRecord::Migration[6.0]
  def change
    create_table :comments do |t |
      #Described from here
      t.references :user,foreign_key: true
      t.references :post, foreign_key: true
      t.text :text,nul: false
      #Described so far
      t.timestamps
    end
  end
end

Migration file adaptation

$ rails db:migrate

Link the Comment model, User model, and Post model

app/models/comment.rb


class Comment < ApplicationRecord
  belongs_to :user
  belongs_to :post
end

app/models/user.rb


class User < ApplicationRecord
  has_many :posts
  has_many :comments
end

app/models/post.rb


class Comment < ApplicationRecord
  has_many :users
  has_many :comments
end

2. Creating a routing

routes.rb


Rails.application.routes.draw do
  resources :users
  resources :posts do
    resource :comments
  end
end

3. Creating a Comments controller

comments_controller.rb


class CommentsController < ApplicationController
  def create
    @comment = Comment.create(comment_params)
    redirect_back(fallback_location: root_path)
  end

 private

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

posts_controller.rb


class PostsController < ApplicationController
  def new
    @post = Post.new
  end

  def create
    @post = Post.new(post_params)
    if @post.save
      redirect_to user_url(current_user)
    else
      render :new
    end
  end
  
  def show
    #Write comment instance creation
    #This time Posts#I would like to implement a comment function in show.
    @comment = Comment.new
    @comments = @post.comment_cs.includes(:user)
  end

private

  def post_params
    params.require(:post).permit(:text).merge(user_id: current_user.id)
  end

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

4. Create a view

Write a description of the comment function on the post details page.

erb:views/posts/show.html.erb



#abridgement

 <%= form_with model: [@post,@comment],merhod: :post,locals: true do | form | %>
    <%= form.text_area :text %>
    <%= form.submit "Post" %>
 <% end %>

#abridgement

Comment function completed!

Summary

This time, I started the comment function in Rails from table creation. I am also a beginner in programming, so please let me know if you make any mistakes. Thank you for reading until the end!

Recommended Posts

Rails [For beginners] Implementation of comment function
[Rails 6] Implementation of search function
[Rails] Implementation of category function
[Rails] Implementation of tutorial function
[Rails] Implementation of like function
Explanation of Ruby on rails for beginners ⑦ ~ Flash implementation ~
[Rails] Implementation of CSV import function
[Rails] Asynchronous implementation of like function
[Rails] Implementation of image preview function
[Rails] About implementation of like function
[Rails] Implementation of user withdrawal function
[Rails] Implementation of CSV export function
[Ruby on Rails] Comment function implementation
[Rails] Comment function implementation procedure memo
[Rails] How to display error messages for comment function (for beginners)
[Rails 6] Implementation of SNS (Twitter) sharing function
Explanation of Ruby on rails for beginners ①
[Vue.js] Implementation of menu function Implementation version rails6
[Ruby on rails] Implementation of like function
[Vue.js] Implementation of menu function Vue.js introduction rails6
Implementation of search function
Rails search function implementation
Implementation of pagination function
[Rails] Implementation of search function using gem's ransack
Implementation of Ruby on Rails login function (Session)
[Rails 6] Implementation of inquiry function using Action Mailer
[Rails] Implementation of image enlargement function using lightbox2
Ruby on Rails <2021> Implementation of simple login function (form_with)
Rails implementation of ajax removal
Rails fuzzy search function implementation
[Rails] Implementation of drag and drop function (with effect)
Implementation of Ruby on Rails login function (devise edition)
Implementation of image preview function
[For Rails beginners] Implemented multiple search function without Gem
[Rails] Implementation of multi-layer category function using ancestry "Preparation"
(For beginners) [Rails] Install Devise
[Rails] Implementation of multi-layer category function using ancestry "seed"
Implementation of category pull-down function
[Rails / devise] Implementation of account information editing function / Procedure for changing redirect destination
Login function implementation with rails
[Rails] Set validation for the search function using Rakuten API (from the implementation of Rakuten API)
[Rails] Implementation of SNS authentication (Twitter, Facebook, Google) function
Explanation of Ruby on rails for beginners ⑥ ~ Creation of validation ~
Explanation of Ruby on rails for beginners ② ~ Creating links ~
[Rails 6] Pagination function implementation (kaminari)
[Rails] Implementation of multi-layer category function using ancestry "Editing form"
[Rails] Implementation of multi-layer category function using ancestry "Creation form"
Ruby on Rails for beginners! !! Summary of new posting functions
[Procedure 1 for beginners] Ruby on Rails: Construction of development environment
Rails sorting function implementation (displayed in order of number of like)
[Rails] Implementation of tagging function using intermediate table (without Gem)
Explanation of Ruby on rails for beginners ③ ~ Creating a database ~
Summary of rails validation (for myself)
[For beginners] Summary of java constructor
About error handling of comment function
Kaminari --Added pagination function of Rails
[Rails] Comment function (registration / display / deletion)
[Rails] Implementation of many-to-many category functions
[Rails] gem ancestry category function implementation
[Rails 6] Like function (synchronous → asynchronous) implementation
Implementation of like function in Java