ruby on railsでレシピ投稿サイトを作成しております。(初学者です。)
投稿したレシピに対してAjaxでのコメント機能を実装中にエラーが発生し、
解決策が見えないため質問させていただきます。
コメント機能実装には以下のサイトを参考にさせていただきました。
Ajaxを用いた動的なコメント投稿・削除機能の実装で学ぶRuby on Rails
発生している問題・エラー
コメントをsubmitすると、Ajaxが機能せず、「Template is missing」のエラー画面となり、
JSファイルにアクセスできていないようです。
コメントの削除は正常に機能しています。
recipes.controller.rb(一部抜粋)
def show @recipe = Recipe.find(params[:id]) @comment = Comment.new @comments = @recipe.comments @like = Like.new end ```### comments.controller.rb ```ここに言語名を入 class CommentsController < ApplicationController before_action :authenticate_user! def create @recipe = Recipe.find(params[:recipe_id]) @comment = @recipe.comments.build(comment_params) @comment.user_id = current_user.id if @comment.save render :js_comment end end def destroy @comment = Comment.find(params[:id]) if @comment.destroy render :js_comment end end private def comment_params params.require(:comment).permit(:content, :recipe_id, :user_id) end end
show.html.slim
.d-flex.align-items-center.mt-4.mb-4 .ml-auto.boards__linkBox = link_to '一覧', recipes_path, class: 'btn btn-outline-dark' = link_to '編集', edit_recipe_path(@recipe), class: 'btn btn-outline-dark' = render @recipe .comment__list .comment__listTitle h3 |コメント #comments_area = render partial: 'comments/index', locals: { comments: @comments } - if user_signed_in? .comment_form = render partial: 'comments/form', locals: { comment: @comment, recipe: @recipe }
_form.html.slim
.comment__formBox .comment__formTitle p | コメントを残す = form_with(model: [recipe, comment] ) do |f| = f.text_area :content, placeholder: "コメントを入力", class: 'form-control', rows: 4, id: "text_area" = f.submit "コメントする!",class: 'btn btn-outline-warning',remote: true
_index.html.slim
- comments.each do |comment| .comment_item - unless comment.id.nil? .comment_user = link_to "#{comment.user.name}さん", comment.user .comment-body = simple_format(comment.content) .comment_delete_btn - if comment.user == current_user = link_to '削除', recipe_comment_path(comment.recipe_id, comment.id), method: :delete, remote: true, class: 'btn btn-outline-secondary'
js_comment.js.erb
$("#comments_area").html("<%= j(render 'index', { comments: @comment.recipe.comments }) %>") $("textarea").val('')
補足情報(FW/ツールのバージョンなど)
rails 5.2
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/12/25 03:06
2019/12/25 05:17
2019/12/30 08:08 編集