[Rails] Comment function implementation procedure memo

At the beginning

A memorandum about the ability to comment on posts

Premise

environment Ruby 2.6 series Rails 5.2 series

Library  devise  Slim

__ Rails app template for the above environment __ Procedure to set up Rails application and install devise and Slim

__ ↓ Image after implementation of comment function ↓ __ ezgif.com-video-to-gif (1).gif

Implementation

1. Model design

スクリーンショット 2020-06-09 21.23.55.png

-User and Post are one-to-many relationships where User has many Posts. -User has many comments, and Post also has many comments. Many-to-many relationship. -Create an intermediate table and add a comment_content column to store the comment owner (user_id), the post that owns the comment (post_id), and the content of the comment for each record.

2. Model creation

$ rails g devise User

$ rails g model Post post_content:string user:references 
$ rails g model Comment comment_content:string user:references post:references

-Since devise is installed, the User model is created with the devise command.

-If you specify references, a foreign key (required) will be automatically set in the migration file.

3. Confirm and associate models and migrations.

users table

class DeviseCreateUsers < ActiveRecord::Migration[5.2] def change create_table :users do |t| ## Database authenticatable t.string :email, null: false, default: "" t.string :encrypted_password, null: false, default: "" . . # . . add_index :users, :email, unique: true add_index :users, :reset_password_token, unique: true end end


 > User model

>```ruby:app/models/user.rb
class User < ApplicationRecord
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable
 
  has_many :posts, dependent: :destroy
  has_many :comments  #User.You can get user-owned comments with comments.
end

posts table

class CreatePosts < ActiveRecord::Migration[5.2] def change create_table :posts do |t| t.text :post_content t.references :user, foreign_key: true   t.timestamps end end end


 > Post model

>```ruby:app/models/post.rb
class Post < ApplicationRecord
  belongs_to :user
  has_many :comments, dependent: :destroy  #Post.You can get the comments w owned by the post with comments.
end

comments table

class CreateComments < ActiveRecord::Migration[5.2] def change create_table :comments do |t| t.text :comment_content t.references :user, foreign_key: true t.references :post, foreign_key: true   t.timestamps end end end


 > Comment model

>```ruby:app/models/comment.rb
class Comment < ApplicationRecord
  belongs_to :user  #Comment.Get the owner of the comment with user
  belongs_to :post  #Comment.Get the post with that comment on post
end

4. Create controller

$ rails g controller posts index show
$ rails g controller comments

4-1. Creating a posts controller

app/controllers/posts_controller.rb


class PostsController < ApplicationController

  #Check the login status of the user. The index can be viewed without logging in.
  before_action :authenticate_user!, only: [:show, :create]

  def index
    @posts = current_user.posts.all  #Get all to display post list
    @post = current_user.posts.new   #Since a new post is made on the post list screen, get the Post object for the form parameter.
  end

  def show
    @post = Post.find(params[:id])
    @comments = @post.comments  #Get all comments associated with post details
    @comment = current_user.comments.new  #Since the comment is posted on the post details screen, get the Comment object for the form parameter.
  end


  def create
    @post = current_user.posts.new(post_params)
    if @post.save
      redirect_back(fallback_location: root_path)  #After sending the comment, redirect to the previous page.
    else
      redirect_back(fallback_location: root_path)  #Same as above
    end
  end

  private
  def post_params
    params.require(:post).permit(:post_content)
  end
end

-The id of the logged-in user is stored in all Posts and Comments acquired as current_user. It can be used because the association made in the model file and devise were introduced.

4-2.comments Creating a controller

app/controllers/comments_controller.rb


class CommentsController < ApplicationController

  def create
    @comment = current_user.comments.new(comment_params)
    if @comment.save
      redirect_back(fallback_location: root_path)  #After sending the comment, redirect to the previous page.
    else
      redirect_back(fallback_location: root_path)  #Same as above
    end
  end

  private
  def comment_params
    params.require(:comment).permit(:comment_content, :post_id)  #post in form_Send id parameter and post to comment_It is necessary to store the id.
  end
end

5. Create view

5-1. Creating a post list view

ruby:app/views/posts/index.html.slim


h2 post

= form_with model: @post do |f|
   = f.text_area :post_content, placeholder: 'Text'
   = f.submit


h2 post list

- @posts.each do |post|
   = link_to post.post_content, post

= link_to 'home', root_path

5-2. Creating a post details screen view (also create a comment posting form and a view of the comment list)

ruby:app/views/posts/show.html.slim


h2 post details

= @post.post_content

h2 make a comment

= form_with(model:[@post, @comment], method: :post) do |f|
  = f.text_area :comment_content
  = f.hidden_field :post_id, value: @post.id
  = f.submit 'To comment'

h2 comment list

- @comments.each do |comment|
  = comment.comment_content  #The content of the comment is displayed

= link_to 'home', root_path

· ``` Model: [@post, @comment]` `` Description for accessing nested routing. If you don't give two arguments properly, it will not be sent properly.

-In the hidden input field, the value post.id is sent with the parameter post_id. To store in the post_id of the Comment table.

6. Create a routing

config/routes.rb


Rails.application.routes.draw do
  devise_for :users  #Routing for user authentication created by devise

  root to: 'posts#index'  #Home screen is set to post list screen

  resources :posts do  #routing to posts controller
    resources :comments, only: [:create]  #comments Routing to controller
  end
end

-By nesting comments resources within posts resources, it is convenient to specify a path such as post_comments_path.

This completes the implementation of the comment function.

Articles that I used as a reference

[Let's make a comment function with Rails](https://qiita.com/nojinoji/items/2034764897c6e91ef982#%E3%83%AB%E3%83%BC%E3%83%86%E3%82%A3% E3% 83% B3% E3% 82% B0% E3% 81% AE% E4% BD% 9C% E6% 88% 90-1)

Recommended Posts

[Rails] Comment function implementation procedure memo
Rails [For beginners] Implementation of comment function
Rails Basic CRUD function implementation procedure scaffold
[rails] gem'payjp' implementation procedure
Comment function (Ajax) implementation
Rails fuzzy search function implementation
[Rails 6] Implementation of search function
[Rails] Implementation of category function
Login function implementation with rails
[Rails] Implementation of like function
[Rails 6] Pagination function implementation (kaminari)
[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
[Rails] Comment function (registration / display / deletion)
[Rails] gem ancestry category function implementation
[Implementation procedure] Create a user authentication function using sorcery in Rails
[Rails] Addition of Ruby On Rails comment function
[Ruby on Rails] Follow function implementation: Bidirectional
[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
DM function implementation
[Rails 6] Ranking function
[Rails] I will explain the implementation procedure of the follow function using form_with.
[Rails] Category function
Rails follow function
Implemented comment function
[Rails] Notification 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 6.0] "Easy login" implementation procedure required for portfolio
[Rails] Implementation of retweet function in SNS application
[JQuery] Implementation procedure of AutoComplete function [Java / Spring]
Ruby on Rails Email automatic sending function implementation
Implementation of search function Learning memo (portfolio creation)
[Rails / devise] Implementation of account information editing function / Procedure for changing redirect destination
[Rails] Implementation procedure when public / private functions are added to the posting function
Rails hashtag search implementation
Ruby on Rails <2021> Implementation of simple login function (form_with)
Rails6 countdown timer implementation
[Rails] Implementation of drag and drop function (with effect)
[Implementation procedure] Implement image upload function with Active Storage
Rails CRUD function implementation ② (edited and detailed this time)
Docker x Rails 6 (memo)
[Rails] Implement search function
Implementation of search function
[Rails] Implemented hashtag function
Rails Heroku deployment procedure
Rails + ElasticSearch Survey Memo
[Ruby on Rails] Implementation of tagging function/tag filtering function
[rails] tag ranking function
Image preview function implementation
[Rails] Implementation of multi-layer category function using ancestry "seed"
Implementation of pagination function
Search function [copype implementation]
Rails, RSpec installation procedure