LoginSignup

Are you sure you want to delete the question?

Leaving a resolved question undeleted may help others!

rails コメント機能の削除をするとルーティングエラーが起きてしまう 初心者のため凡ミスかもしれません

解決したいこと

コメント機能を導入してみたのですが、
コメントの削除を行うとルーティングエラーが発生してしまい削除ができないため
コメントを削除できるようにしたいです

https://qiita.com/oak1331/items/a75d394e652d3e857bff
この記事を参考にコメント機能を追加しました

発生している問題・エラー

コメント機能のコメント削除を行うとルーティングエラーが生じてしまいコメントの削除ができない

Routing Error
No route matches [GET] "/posts/4/comments/6"

該当するソースコード

show.erb.html
    <% if @current_user %>
    <%= form_with model: [@post, @comment], local: true do |form| %>
      <%= form.text_area :text, placeholder: "コメントする", rows: "2" %>
      <%= form.submit "送信" %>
    <% end %>
  <% else %>
    <strong><p>コメントの投稿には新規登録/ログインが必要です</p></strong>
  <% end %>
  
  <h4><コメント一覧></h4>
  <% @comments.each do |comment| %>
    <p>
      <strong><%= comment.user.name %></strong>
      <%= comment.text %>
      <% if @current_user && comment.user_id == @current_user.id %>
        <%= link_to "削除", post_comment_path(comment.post.id, comment.id), method: :delete %>
      <% end %>
    </p>
  <% end %>

comments_controller.rb
    before_action :authenticate_user

  def create
    @comment = Comment.new(comment_params)
    if @comment.save
      redirect_to("/posts/#{params[:post_id]}")
    else
      @post = @comment.post
      @comments = @post.comments.includes(:user)
      render "posts/show"
    end
  end

  def destroy
    comment = Comment.find_by(id: params[:id], post_id: params[:post_id])
    comment.destroy
    redirect_to("/posts/#{params[:post_id]}")
  end



  private
  def comment_params
    params.require(:comment).permit(:text).merge(user_id: @current_user.id, post_id: params[:post_id])
  end

routes.rb
 resources :posts, only: [] do
    resources :comments, only: [:create, :destroy]
  end
comment.rb
    belongs_to :user
    belongs_to :post

    validates :text, presence: true

最後まで読んでいただいてありがとうございます
ご教授いただけると幸いです

1

1Answer

記載はないので状況からRails7を使用していると推測して回答します。

Rails7では link_todeleteメソッドの指定は動かないようです。
https://github.com/rails/rails/issues/44185
背景としてはRails7ではturboという機能が入っているためです。

対応としては次のいずれかになると思います。

  1. button_toでdeleteメソッドを指定する(おすすめ)
  2. link_toの引数でdata: { "turbo-method": :delete }を追加する
    1. https://github.com/heartcombo/devise/issues/5439#issuecomment-997927041
  3. config/routes.rbでコメント削除用のルーティングをGETで新たに作成する(おすすめしない)
1

Comments

  1. @NetaNeta0620

    Questioner
    回答していただきありがとうございます!!
    >1.button_toでdeleteメソッドを指定する(おすすめ)
    ```
    <%= button_to '削除', {controller: 'comments', action: 'destroy'}, {method: :delete }%>
    ```
    をすると
    ```error
    ActionController::UrlGenerationError in Posts#show
    ```
    と出てしまいます

    >2.link_toの引数でdata: { "turbo-method": :delete }を追加する

    ```
    <%= link_to "削除", post_comment_path(comment.post.id, comment.id), data: { "turbo-method": :delete } %>
    ```
    ```
    Routing Error
    No route matches [GET] "/posts/1/comments/1"
    ```
    とやはりエラーが出てしまいます

    このコードが書き間違えているのでしょうか?
  2. @NetaNeta0620

    Questioner
    ありがとうございます!!!!!!!
    教えていただいたコードに変更したところコメント削除することができました!
    本当に助かりました!!!

Your answer might help someone💌