[Ruby on Rails] 주석 기능 설치

14072 단어 CommentRubyRails

목표



개발 환경


ruby 2.5.7
Rails 5.2.4.3
OS: macOS Catalina

전제 조건


※ ▶◯◯을 선택하면 설명 등이 나온다
잘 모르는 상황의 참고가 되었으면 좋겠습니다.
  • devise를 통한 로그인 환경 구축
  • 로그인한 사용자만 수행할 수 있는 발언 기능
  • 발언 편집 기능(업데이트, 삭제)
  • 테이블 작성


    단말기
    $ 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는 여러 개의 공공 모델을 가지고 있기 때문에post는 여러 개의 공공 모델을 가지고 있다
    has_many.
    또한user,post가 없을 때commeent도 남을 필요가 없습니다
    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 이외의 동작을 정의합니다.

    뷰 변경


    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>
    
    목표를 달성하다.

    좋은 웹페이지 즐겨찾기