Home>

The comment function is being implemented.
Other users can also delete the comments that they have commented on.
I want to be able to delete only the comments I wrote
<% if c.user%>
I tried to do it, but I can't put a limit on it.
Besides,<% if c.user_id%>
I tried it, but it didn't change. I would like to know.
I look forward to working with you.

posts/show.html

    <p>Comment list</p>
    <% @ comments.each do | c |%>
        
        <% unless c.user.blank?%>
           ">
        <% end%>
        <% = c.user.name unless c.user.blank?%>
        <br />
        <% = c.content%>
        <% if c.user%>
         <% = link_to "delete", comment_path (c), method:: delete, class: "comment-menus"%>
        <% end%>
        
      <br />
posts_controller
def index
    @posts = Post.all.order (created_at :: desc)
  end
  def show
    @post = Post.find_by (id: params [: id])
    @user = @ post.user
    @post = Post.find (params [: id])
    @comments = @ post.comments
    @comment = Comment.new
  end
  def new
    @post = Post.new
  end
  def create
    @post = Post.new (
      content: params [: content],
      user_id: @ current_user.id,
     )
     if params [: post] .present?
      @ post.video = params [: post] [: video]
      print params
     end
    if @ post.save
     flash [: notice] = "Created a post"
     redirect_to ("/ posts/index")
    else else
     render ("posts/new")
    end
  end
 def edit
   @post = Post.find_by (id: params [: id])
 end
 def update
    @post = Post.find_by (id: params [: id])
    @ post.content = params [: content]
   if @ post.save
    flash [: notice] = "Edited post"
    redirect_to ("/ posts/index")
   else else
    render ("posts/edit")
   end
 end
 def destroy
   @post = Post.find_by (id: params [: id])
   @ post.destroy
   flash [: notice] = "Post deleted"
   redirect_to ("/ posts/index")
 end
 def ensure_correct_user
     @post = Post.find_by (id: params [: id])
     if @ post.user_id! = @ current_user.id
       flash [: notice] = "You are not authorized"
       redirect_to ("/ posts/index")
     end
 end
end
  • Answer # 1

    Excuse me
    <% if c.user == current_user.id%>
    I solved it here!
    Thank you!

  • Answer # 2

    I don't know what the controller looks like, but how about this description?
    <% if c.user_id == current_user.id%>

Related articles