※他のサイトでも同様の質問を行っています。解決後はそれぞれ更新致します。よろしくお願いします。
1.前提・実現したいこと
投稿された質問(post)に対して回答したコメント(comment)に非同期通信でいいね(good)を押す機能を実装したいです。
2.発生している問題・エラーメッセージ
設置したいいねリンクでcreateアクションが実行されません。
ビューは表示されており、検証ツールで以下のエラーが生じています。
画面のハートを押すとこのような挙動になります。
rails-ujs.js:216 POST http://localhost:3000/posts/1/comments/8/goods 404 (Not Found)
3.該当のソースコード
ビュー関連
app/views/goods/create.js.erb
1$('#goods_buttons<%= @comment.id %>').html("<%= j(render partial: 'goods/good', locals: {post: @post, comment: @comment}) %>");
app/views/goods/destroy.js.erb
1$('#goods_buttons<%= @comment.id %>').html("<%= j(render partial: 'goods/good', locals: {post: @post, comment: @comment}) %>");
app/views/goods/_good.html.erb
1<% unless @comment.good_user(@current_user.id).blank? %> 2 <%= link_to post_comment_good_path(post_id: @post.id, comment_id: comment.id), method: :delete, remote: true do %> 3 <div class="vertical_good"> 4 <%= image_tag "icon_red_heart.png", size: '20x20' %> 5 </div> 6 <% end %> 7 <% else %> 8 <%= link_to post_comment_goods_path(@post, comment), method: :post, remote: true do %> 9 <div class="vertical_good"> 10 <%= image_tag "icon_heart.png", size: '20x20' %> 11 </div> 12 <% end %> 13 <% end %>
app/views/comments/_comment.html.erb
1<div class="p-comment__item"> 2 <p><%= simple_format(comment.comment) %></p> 3 <div class="p-comment__bottomLine"> 4 <div id="goods_buttons<%= comment.id %>"> 5 <%= render partial: 'goods/good', locals: { post: @post, comment: comment } %> 6 </div> 7 <span><%= comment.created_at.to_s(:datetime_jp) %></span> 8 <span><%= link_to '削除', post_comment_path(@post, comment), method: :delete, data: { confirm: '削除してよろしいですか?' } %></span> 9 </div> 10</div> 11
app/views/posts/show.html.erb
1(一部省略) 2<div class="p-comment__list"> 3 <div class="p-comment_listTitle">コメント</div> 4 <%= render @post.comments %> 5</div> 6 7<%= render partial: 'comments/form', locals: { comment: @comment } %>
コントローラ
goods_controller.rb
1class GoodsController < ApplicationController 2 before_action :set_comment, only: %i[create destroy] 3 4 def create 5 @good = Good.create(user_id: current.user_id, post_id: @post.id, comment_id: @comment.id) 6 end 7 8 def destroy 9 good = Good.find_by(user_id: current.user_id, comment_id: comment.id) 10 good.destroy 11 end 12 13 private 14 15 def set_comment 16 @post = Post.find(params[:id]) 17 @comment = Comment.find(comment_id: @post.id) 18 end 19end
comments_controller.rb
1class CommentsController < ApplicationController 2 def create 3 comment = Comment.new(comment_params) 4 if comment.save 5 flash[:notice] = 'コメントを投稿しました' 6 redirect_to comment.post 7 else 8 flash[:comment] = comment 9 flash[:error_messages] = comment.errors.full_messages 10 redirect_back fallback_location: comment.post 11 end 12 end 13 14 def destroy 15 comment = Comment.find(params[:id]) 16 comment.delete 17 redirect_to comment.post, flash: { notice: 'コメントが削除されました' } 18 end 19 20 private 21 22 def comment_params 23 params.require(:comment).permit(:post_id, :comment) 24 end 25end
posts_controller.rb
1class PostsController < ApplicationController 2 before_action :set_target_post, only: %i[show edit update destroy] 3 4(省略) 5 6 def show 7 @comment = Comment.new(post_id: @post.id) 8 end 9 10(省略) 11 12 private 13 14(省略) 15 16 def set_target_post 17 @post = Post.find(params[:id]) 18 end 19end
4.自分で調べたことや試したこと
質問に対してのいいねは以下記事を参考にし、実行できました。
https://qiita.com/max_3252/items/ca8f623563e26b6a8630
次はそのコードを流用する形でコメントへのいいねを実装しようとしましたが、このようなエラーが生じています。
404 not foundについて調べたところ、リンク切れやリダイレクトの設定ミスとありました。
リンク切れは無いとして、リダイレクトの設定ミスかと模索しました。
エラー内容にて、http://localhost:3000/posts/1/comments/8/goods とあり、
methodの指定も行なっているので問題ないのでは...と思い、原因を掴めていません。
5.使っているツールのバージョンなど補足情報
ruby: 3.0.2
rails: 6.0.4
jquery-rails: 4.4.0
回答1件