AJaXでのコメント削除機能がうまく動きません。
どこをどう直せばいいですか?
createのほうはdestroyと同じコードでうまくいきました。
commentcontroller
1class BookCommentsController < ApplicationController 2 def create 3 @book_show = Book.find(params[:book_id]) 4 @book_comment = current_user.book_comments.build(book_comment_params) 5 @book_comment.book_id = @book_show.id 6 @book_comment.save 7 end 8 9 def destroy 10 @book_comment = BookComment.find_by(id: params[:id], book_id: params[:book_id]) 11 @book_comment.destroy 12 end 13 14 private 15 def book_comment_params 16 params.require(:book_comment).permit(:comment) 17 end 18end
index
1% book.book_comments.each do |book_comment| %> 2 <li class="comment"> 3 <div class="comment-body bb"> 4 <div class="comment-avatar"> 5 <div class="avatar"> 6 <%= attachment_image_tag @user_show, :profile_image, format: 'jpeg', size: '50x50', fallback: "no_image.jpg" %> 7 </div> 8 <div class="comment-text"> 9 <div class="comment-author clearfix"> 10 <a href="#" class="link-author"><%= book_comment.user.name %></a> 11 <span class="comment-meta"> 12 <span class="comment-date"><%= book_comment.created_at.strftime('%Y/%m/%d') %></span> 13 </span> 14 </div> 15 <div class="comment-entry"><%= book_comment.comment %></div> 16 </div> 17 <% if book_comment.user == current_user %> 18 <div> 19 <%= link_to "delete", book_book_comment_path(book_comment.book_id, book_comment.id), method: :delete, remote: true, class: "btn btn-danger pull-rigth" %> 20 </div> 21 <% end %> 22 </div> 23 </div> 24 </li> 25<% end %>
destroyjs
1$("#comments_area").html("<%= j(render 'book_comments/index', { comments: @book_comment.book.book_comments, book: @book_show }) %>") 2$("textarea").val('')
あなたの回答
tips
プレビュー