LoginSignup
5
7

More than 3 years have passed since last update.

【Ruby on Rails】コメント機能実装

Last updated at Posted at 2020-09-04

目標

コメント.gif

開発環境

ruby 2.5.7
Rails 5.2.4.3
OS: macOS Catalina

前提

※ ▶◯◯ を選択すると、説明等が出てきますので、
  よくわからない場合の参考にしていただければと思います。

テーブルの作成

ターミナル
$ rails g model Comment user:references post:references comment:string
ターミナル
$ rails db:migrate

モデルの修正

app/models/user.rb
has_many :comments, dependent: :destroy
app/models/post.rb
has_many :comments, dependent: :destroy

補足
userは複数のCommentモデル、postは複数のCommentモデルを保有するため、
has_many。
またuser、postがなくなった時はcommentも残す必要はないため、
dependent: :destroy。

コントローラーの作成

ターミナル
$ rails g controller comments create destroy
app/controllers/comments_controller.rb
class CommentsController < ApplicationController
  def create
    @post = Post.find(params[:post_id])
    @comment = @post.comments.new(comment_params)
    @comment.user_id = current_user.id
    if @comment.save
      redirect_to request.referer
    else
      @post_new = Book.new
      @comments = @post.comments
      redirect_to new_post_path
    end
  end

  def destroy
    @post = Post.find(params[:post_id])
    @comment = Comment.find(params[:id])
    @comment.destroy
    redirect_to request.referer
  end

  private

  def comment_params
    params.require(:comment).permit(:comment)
  end
end

app/controllers/posts_controller.rb
  def show
    @post = Post.find(params[:id])
    @comment = Comment.new
    @comments = @post.comments
  end

ルーティングの修正

config/routes.rb
  resources :posts, except: [:index] do
    resources :comments, only: [:create, :destroy]
  end

補足1
上記記述はネストさせています。
ネストについてはこちらがわかりやすかったです。

補足2
exceptは除くという意味なので、index以外のアクションを定義しています。

viewsの変更

app/views/show.html.erb
<h1>Posts#show</h1>
<span>現在ログイン中のユーザー:<%= current_user.name %></span>

<table>
  <thead>
    <tr>
      <th>投稿者名</th>
      <th>タイトル</th>
      <th>本文</th>
      <th></th>
      <th></th>
      <th></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td><%= @post.user.name %></td>
      <td><%= @post.title %></td>
      <td><%= @post.body %></td>
      <td><%= link_to "編集", edit_post_path(@post) %></td>
    </tr>
  </tbody>
</table>

<%= form_for [@post, @comment] do |f| %>
  <%= f.text_area :comment, size: "40x5" %>
  <%= f.submit '送信', class: "btn-sm btn-primary" %>
<% end %>

<table>
  <thead>
    <tr>
      <th>コメント投稿者</th>
      <th>コメント内容</th>
    </tr>
  </thead>
  <tbody>
    <% @comments.each do |comment| %>
      <tr>
        <td><%= comment.user.name %></td>
        <td><%= comment.comment %></td>
        <td><%= link_to "削除", post_comment_path(@post, comment), method: :delete %></td>
      </tr>
    <% end %>
  </tbody>
</table>
app/views/new.html.erb
<table>
  <thead>
    <tr>
      <th>投稿者名</th>
      <th>タイトル</th>
      <th>本文</th>
      <th></th>
      <th></th>
      <th></th>
    </tr>
  </thead>
  <tbody>
    <% @posts.each do |post| %>
      <tr>
        <td><%= post.user.name %></td>
        <td><%= post.title %></td>
        <td><%= post.body %></td>
        <td><%= link_to "詳細", post_path(post) %></td>
        <td><%= link_to "編集", edit_post_path(post) %></td>
        <td><%= link_to "削除", post_path(post), method: :delete %></td>
      </tr>
    <% end %>
  </tbody>
</table>

目標完成。

5
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
7