Home>

Added a function to comment on a post on the post details page.
Then add the delete comment function and press delete
ActiveRecord :: RecordNotFound in TweetsController # destroy
Couldn't find Tweet with 'id' = 27
I get an error.
You are in the destroy action of tweets.controller.rb.
I would like to skip to comments.controller.rb's destroy ...
What should I do?

file views/commets/comment.html.haml
.comments
    % h4<Comment list>
    -if @comments
      [email protected] comments.each do | comment |
        % p
          % strong
            = link_to comment.user.nickname, "/users/#{comment.user_id}"
            :
          = comment.text
        -if user_signed_in?&&current_user.id == comment.user_id
          = link_to "delete", "/ tweets/# {comment.id}", method:: delete, class: "image-delete"
file controllers/comments_controller.rb
class CommentsController</pre>
<pre><code>file routes.rb
resources: tweets do
    resources: comments, only: [: create,: destroy]
  end
  • Answer # 1

    = link_to "delete", "/ tweets/# {comment.id}", method:: delete, class: "image-delete"

    is "/ tweets. Please change it to comments.

  • Answer # 2

    = link_to" delete ","/comments/# {comment.id} ", method:: delete , class: "image-delete"
    Commets plural to

    File name
    resources: tweets do
    resources: comments, only: [: create]
    end
    resources: comments, only: [: destroy]

    Remove from nesting

    def destroy
    comment = Comment.find (params [: id])
    comment.destroy
    redirect_to tweets_path
    end

    Add redirect_to

    This will clear all errors

Related articles