[Ruby on Rails] Implementierung der Kommentarfunktion

Ziel

コメント.gif

Entwicklungsumgebung

ruby 2.5.7 Rails 5.2.4.3 OS: macOS Catalina

Annahme

Eine Tabelle erstellen

Terminal


$ rails g model Comment user:references post:references comment:string

Terminal


$ rails db:migrate

Modelländerung

app/models/user.rb


has_many :comments, dependent: :destroy

app/models/post.rb


has_many :comments, dependent: :destroy
Supplement Da der Benutzer mehrere Kommentarmodelle hat und der Beitrag mehrere Kommentarmodelle hat, has_many。 Wenn keine Benutzer oder Beiträge mehr vorhanden sind, müssen keine Kommentare hinterlassen werden dependent: :destroy。
# Controller erstellen

Terminal


$ 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

Routing-Fix

config/routes.rb


  resources :posts, except: [:index] do
    resources :comments, only: [:create, :destroy]
  end
Ergänzung 1 Die obige Beschreibung ist verschachtelt. Über das Verschachteln war [hier](https://qiita.com/chopesu_se/items/c7362380865cf978b158) leicht zu verstehen.
Supplement 2 Da es bedeutet, Ausnahme auszuschließen, definiert es eine andere Aktion als Index. # Ansichten ändern

erb:app/views/show.html.erb


<h1>Posts#show</h1>
<span>Derzeit angemeldeter Benutzer:<%= current_user.name %></span>

<table>
  <thead>
    <tr>
      <th>Gepostet mit Namen</th>
      <th>Titel</th>
      <th>Text</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 "Bearbeiten", edit_post_path(@post) %></td>
    </tr>
  </tbody>
</table>

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

<table>
  <thead>
    <tr>
      <th>Kommentar beitragender</th>
      <th>Inhalt kommentieren</th>
    </tr>
  </thead>
  <tbody>
    <% @comments.each do |comment| %>
      <tr>
        <td><%= comment.user.name %></td>
        <td><%= comment.comment %></td>
        <td><%= link_to "Löschen", post_comment_path(@post, comment), method: :delete %></td>
      </tr>
    <% end %>
  </tbody>
</table>

erb:app/views/new.html.erb


<table>
  <thead>
    <tr>
      <th>Gepostet mit Namen</th>
      <th>Titel</th>
      <th>Text</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 "Einzelheiten", post_path(post) %></td>
        <td><%= link_to "Bearbeiten", edit_post_path(post) %></td>
        <td><%= link_to "Löschen", post_path(post), method: :delete %></td>
      </tr>
    <% end %>
  </tbody>
</table>

Das Ziel ist erreicht.

Recommended Posts