※他質問サイトでも同様の質問をしています。解決した場合はそちらも更新します。よろしくお願いします。
1.前提・実現したいこと
投稿された質問(post)に対して回答したコメント(comment)に非同期通信でいいね(good)を押す機能を実装したいです。
空のハートマークを押すと赤いハートに切り替わり、赤いハートを押すと空のハートマークに切り替わります。
2.発生している問題・エラーメッセージ
いいねを押した後にリロードすると空のハートマークになってしまいます。
そのまま赤いハートに残っていて欲しいです。
②いいねを押した後
テーブルに作成され、赤いハートに切り替わります。
id:20, comment_id: 8, user_id: 1
③リロードした後(問題がある点)
テーブルには作成されたままですが、空のハートになってしまいます。
④もう一度いいねを押す
テーブルの中身は変わらず、赤いハートに切り替わります。
id:20, comment_id: 8, user_id: 1
⑤赤いハートを押す
テーブルの中身は空になり、空のハートに切り替わります。
3.該当のソースコード
ビュー関連
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, id: comment.goods[0].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>
app/views/posts/show.html.erb
1(省略) 2 3<div class="p-comment__list"> 4 <div class="p-comment_listTitle">コメント</div> 5 <%= render @post.comments %> 6</div> 7 8<%= render partial: 'comments/form', locals: { comment: @comment } %>
app/views/goods/create.js.erb
1$('#goods_buttons<%= @comment.id %>').html("<%= j(render partial: 'goods/good', locals: {comment: @comment}) %>");
app/views/goods/destroy.js.erb
1$('#goods_buttons<%= @comment.id %>').html("<%= j(render partial: 'goods/good', 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, 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[:post_id]) 17 @comment = Comment.find(params[:comment_id]) 18 end 19end
モデル関連
comment.rb
1class Comment < ApplicationRecord 2 belongs_to :post 3 has_many :goods, dependent: :destroy 4 5 validates :comment, presence: true, length: { maximum: 1000 } 6 7 def good_user(user_id) 8 goods.find_by(user_id: user_id) 9 end 10end
good.rb
1class Good < ApplicationRecord 2 belongs_to :comment 3 belongs_to :user 4 5 validates_uniqueness_of :comment_id, scope: :user_id 6end
user.rb
1class User < ApplicationRecord 2(省略) 3 has_many :favorites 4 has_many :goods 5 6(省略) 7end
4.自分で調べたことや試したこと
テーブルには保存されているので、comment.rbのgood_user(user_id)メソッドがおかしいかと思いましたが、特に間違いは見つけられず
そのメソッドを使用している_good.html.erbに問題があるのかと模索しましたが、わかりませんでした。
エラーメッセージが表示されているわけではないため原因の検討がつかず、知恵をお借りしたいです。
5.使っているツールのバージョンなど補足情報
ruby: 3.0.2
rails: 6.0.4
jquery-rails: 4.4.0

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/02/20 10:30
2022/02/20 13:37
2022/02/21 02:30