Home>

Press Delete comment
ActiveRecord :: RecordNotFound in TweetsController # destroy
Couldn't find Tweet with 'id' = 26
I get an error.

tweets.controller.rb

def destroy
tweet = Tweet.find (params [: id])
tweet.destroy
end
ActiveRecord :: RecordNotFound seems to come out trying to read the action of

Which part should I correct?

file name<routes.rb>
resources: tweets do
  resources: comments, only: [: create,: destroy]
end
file name<show.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 name<comments.controller.rb>
def destroy
    comment = Comment.find (params [: id])
    comment.destroy
end
File name<comments.controller.rb>All descriptions
def create
    @comment = Comment.create (text: comment_params [: text], tweet_id: comment_params [: tweet_id], user_id: current_user.id)
    respond_to do | format |
      format.html {redirect_to tweet_path (params [: tweet_id])}
      format.json
    end
  end
  def destroy
    comment = Comment.find (params [: id])
    comment.destroy
  end

  private
  def comment_params
    params.require (: comment) .permit (: text) .merge (user_id: current_user.id, tweet_id: params [: tweet_id])
  end
  • Answer # 1

    = 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

  • Answer # 2

    link_to "delete", "/ tweets/# {comment.id}"instead oflink_to "delete", " I think it works with/tweets/# {tweet ID}/comments/# {comment.id} ".