コメント機能の非同期通信化

現在、プログラミング学習中(1ヶ月)です。
分かりづらい部分があるかと思いますが、ご指摘頂けると幸いです。

本の投稿機能があるアプリケーションを作成しております。
実装した機能は、ログイン機能(device)・本の投稿(削除)機能・コメント機能・いいね機能・フォロー(フォロワー)機能です。
いいね機能を非同期通信化し、今度はコメント機能も非同期通信化をおこないたいのですが、
なかなか上手くいかず、自力で解消できません。
お力添え頂ければと思いご質問させて頂いております。

-------------------------------------------- エラー文始まり --------------------------------------------
ActionController::UnknownFormat in BookCommentsController#create
BookCommentsController#create is missing a template for this request format and variant.
request.formats: ["text/html"]
request.variant: []

Extracted

Rails.root: /home/ec2-user/environment/Rails6-bookers2-debug-main
-------------------------------------------- エラー文終わり --------------------------------------------

該当コード

books_controller.rb↓

class BooksController < ApplicationController before_action :authenticate_user! def show @book_new = Book.new @book = Book.find(params[:id]) @user = @book.user @book_comment = BookComment.new end def index @book = Book.new @books = Book.all end def create @book = Book.new(book_params) @book.user_id = current_user.id if @book.save redirect_to book_path(@book), notice: "You have created book successfully." else @books = Book.all render 'index' end end def edit @book = Book.find(params[:id]) if @book.user == current_user else redirect_to books_path end end def update @book = Book.find(params[:id]) if @book.update(book_params) redirect_to book_path(@book), notice: "You have updated book successfully." else render "edit" end end def destroy @book = Book.find(params[:id]) @book.destroy redirect_to books_path end private def book_params params.require(:book).permit(:title, :body) end end

book_comments_controller.rb↓

class BookCommentsController < ApplicationController def create comment = current_user.book_comments.new(book_comment_params) comment.book_id = Book.find(params[:book_id]).id comment.save end def destroy BookComment.find(params[:id]).destroy end private def book_comment_params params.require(:book_comment).permit(:comment) end end

books/show.html.erb↓

<div class='container'> <div class='row'> <div class='col-md-3'> <%= render 'users/info', user: @user %> <h2 class="mt-3">New book</h2> <%= render 'form', book: @book_new %> </div> <div class='col-md-8 offset-md-1'> <h2>Book detail</h2> <table class='table'> <tr> <td><%= link_to(@book.user) do %> <%= image_tag @book.user.get_profile_image, size:"100x100" %><br> <%= @book.user.name %> <% end %> </td> <td><%= link_to @book.title %></td> <td><%= @book.body %></td> <td> <% if @book.user == current_user %> <%= link_to 'Edit', edit_book_path(@book), class: "btn btn-sm btn-success edit_book_#{@book.id}" %> <% end %> </td> <td> <% if @book.user == current_user %> <%= link_to 'Destroy', @book, method: :delete, data: { confirm: '本当に消しますか?' }, class: "btn btn-sm btn-danger destroy_book_#{@book.id}"%> <% end %> </td> <td class="favorite-btn_<%= @book.id %>"> <%= render 'favorites/favorite-btn', book: @book %> </td> <td> コメント数:<%= @book.book_comments.count %> </td> </tr> </table> <div class="comment"> **<%= render 'book_comments/comment-index', book: @book %>** </div> <div> **<%= render 'book_comments/comment-new', book: @book, book_comment: @book_comment %>** </div> </div> </div> </div>

※該当箇所は最下部のrender太文字2行です。

book_comments/_comment-index.html.erb↓

<% book.book_comments.each do |book_comment| %> <table> <tr> <td> <%= image_tag book_comment.user.get_profile_image, size:"100x100" %><br> <%= book_comment.user.name %> </td> <td><%= book_comment.comment %></td> <td> <% if book_comment.user == current_user %> <%= link_to "destroy", book_book_comment_path(book_comment.book, book_comment), method: :delete, remote: true, class: "btn btn-sm btn-danger" %> <% end %> </td> </tr> </table> <% end %>

book_comments/_comment-new.html.erb↓

<%= form_with model: [book, book_comment], remote: true do |f| %> <%= f.text_area :comment, rows: '5' %> <%= f.submit "送信" %> <% end %>

book_comments/destroy(create).js.erb↓

$(".comment-index").html("<%= j(render 'book_comments/comment-index', book: @book) %>"); $(".comment-new").html("<%= j(render 'book_comments/comment-new', book: @book, book_comment: @book_comment ) %>");

コメントを投稿

0 コメント