[Rails] Addition of Ruby On Rails comment function

conditions

Only logged-in users can post and comment. Only the creator can delete the comment.

table

Screen Shot 2020-10-09 at 18.17.06.png

model

post.rb


class Post < ApplicationRecord
  belongs_to :user
  has_many :comments
end

comment.rb


class Comment < ApplicationRecord
  belongs_to :post
end

routing

You can use post_id and id (of user) in the parameters.

routes.rb


Rails.application.routes.draw do
 devise_for :users
  resources :posts do
    resources :comments, only: [:create, :destroy]
  end
  root "posts#index"
end
post_comments POST   /posts/:post_id/comments(.:format)     comments#create
post_comment DELETE /posts/:post_id/comments/:id(.:format) comments#destroy

controller

comments_controller.rb


def create
    @post = Post.find(params[:post_id])
    @post.comments.create(comment_params)
    redirect_to post_path(@post)
  end

  def destroy
    @post = Post.find(params[:post_id])
    @comment = @post.comments.find(params[:id])
    @comment.destroy
    redirect_to post_path(@post)
  end

  private
    def comment_params
      params.require(:comment).permit(:body, :user_id)
    end

View

The id of the user created by <% = f.hidden_field: user_id, value: current_user.id%> is passed.

show.html.erb


<p>
  <strong>Post:</strong>
  <%= @post.post %>
</p>

<% if @post.user_id == current_user.id %>
<%= link_to 'Edit', edit_post_path(@post) %> |
<% end %>
<%= link_to 'Back', posts_path %>

<h3>Comments</h3>
<% @post.comments.each do |comment|%>
<ul>
  <li><%= comment.body %>
  <span>
  <% if comment.user_id == current_user.id %>
  <%= link_to '[X]', post_comment_path(@post, comment), method: :delete %>
  <% end %>
  </span>
  </li>
</ul>
<% end %>

<%= form_for [@post, @post.comments.build] do |f| %>
<div class="field">
  <%= f.text_field :body, autofocus: true, autocomplete: "body" %>
</div>
<div class="field">
  <%= f.hidden_field :user_id, value: current_user.id %>
</div>
<div class="actions">
  <%= f.submit %>
</div>
<% end %>

Recommended Posts

[Rails] Addition of Ruby On Rails comment function
[Ruby on Rails] Comment function implementation
[Ruby on rails] Implementation of like function
Implementation of Ruby on Rails login function (Session)
Ruby on Rails <2021> Implementation of simple login function (form_with)
[Ruby on Rails] Asynchronous communication of posting function, ajax
Implementation of Ruby on Rails login function (devise edition)
[Ruby on Rails] Implementation of tagging function/tag filtering function
[Ruby on Rails] Introduced paging function
Basic knowledge of Ruby on Rails
[Ruby on Rails] CSV output function
[Ruby on Rails] DM, chat function
A note about the seed function of Ruby on Rails
[Ruby on Rails] Introduction of initial data
Rails Addition of easy and easy login function
Let's summarize "MVC" of Ruby on Rails
part of the syntax of ruby ​​on rails
[Ruby on Rails] Follow function implementation: Bidirectional
Rails [For beginners] Implementation of comment function
[Ruby on Rails] Japanese notation of errors
Explanation of Ruby on rails for beginners ①
[Ruby on Rails] Logical deletion (withdrawal function)
Ruby on Rails Elementary
Ruby on Rails basics
Ruby On Rails Association
Validation settings for Ruby on Rails login function
[Ruby on Rails] Until the introduction of RSpec
Recommendation of Service class in Ruby on Rails
Ruby on Rails ~ Basics of MVC and Router ~
[Ruby on Rails] A memorandum of layout templates
Ruby on Rails Email automatic sending function implementation
[Ruby on Rails] Post editing function (update, delete)
[Ruby on Rails] Individual display of error messages
Addition of guest login function
Ruby on rails learning record -2020.10.03
Portfolio creation Ruby on Rails
Ruby on rails learning record -2020.10.04
Docker the development environment of Ruby on Rails project
[Ruby on Rails] Debug (binding.pry)
Ruby on rails learning record -2020.10.05
Ruby on rails learning record -2020.10.09
Ruby on Rails config configuration
Ruby on Rails basic learning ①
[Rails 6] Implementation of search function
[Ruby on Rails] Posting score ranking function (whole display)
[Ruby on Rails] about has_secure_password
Ruby on rails learning record-2020.10.07 ②
[Rails] Implementation of category function
[Ruby on Rails] Post image preview function in refile
Commentary on partial! --Ruby on Rails
Ruby on rails learning record-2020.10.07 ①
Cancel Ruby on Rails migration
[Rails] Implementation of tutorial function
Ruby on rails learning record -2020.10.06
Explanation of Ruby on rails for beginners ⑥ ~ Creation of validation ~
Explanation of Ruby on rails for beginners ② ~ Creating links ~
[Rails] Implementation of like function
Ruby on Rails validation summary
[Ruby on Rails] Search function (model, method selection formula)
Ruby on Rails Basic Memorandum
Try using the query attribute of Ruby on Rails