Ruby on Railsで非同期通信のいいね機能の実装に取り組んでいますが
エラーが出て非同期でいいねができない状態です。
現在は同期通信ではいいねはできます。
解決方法を教えて下さい。
発生している問題・エラー
ActionView::Template::Error (undefined method `id' for nil:NilClass):
$("#like_heart_<%= @post.id %>").html("<%= j(render 'likes/like', locals: {post: @post}) %>");
app/views/likes/create.js.erb:1
自分で試したこと
ここに問題・エラーに対して試したことを記載してください。
<%= @post.id %>ここが間違っているかと仮定して変更したりしましたが
実装にまでたどりつきません。
お時間ある方お願いします。
likes_controller.rb
1class LikesController < ApplicationController 2 def create 3 post = Post.find(params[:post_id]) 4 @like = Like.create(user_id: current_user.id, post_id: post.id) 5 end 6 7 def destroy 8 post = Post.find(params[:post_id]) 9 @like = Like.find_by(user_id: current_user.id, post_id: post.id) 10 @like.destroy 11 end 12end
show.html.erb
1<main class="main"> 2<div class= "null"> 3</div> 4<div id="map"></div> 5 <script 6 src="https://maps.googleapis.com/maps/api/js?key=<%=ENV['GOOGLE_MAP_KEY']%>&callback=initMap&v=weekly" 7 async 8 ></script> 9 <script> 10 let map; 11 12function initMap() { 13 map = new google.maps.Map(document.getElementById("map"), { 14 center: { lat: <%= @post.latitude %>, lng: <%= @post.longitude %>}, 15 zoom: 15, 16 }); 17 var marker = new google.maps.Marker({ 18 position: { lat: <%= @post.latitude %>, lng: <%= @post.longitude %>}, 19 map: map, 20 title: "<%= @post.address %>" 21 }); 22} 23 </script> 24 25 <div class="inner"> 26 <div class="post__wrapper"> 27 <div class="post_show_image"> 28 <%= image_tag @post.image.variant(resize: '500x500'), class: "post_show_image" %> 29 </div> 30 <div class="post__body"> 31 <div class="post_address"> 32 <%= @post.address %> 33 <div id="like_heart_<%= @post.likes %>"> 34 <%= render partial: "likes/like", locals: { post: @post, likes: @likes } %> 35 </div> 36 </div> 37 <div class="post_spring_quality"> 38 <div class="detail__message"> 39 泉質: 40 <%= @post.spring_quality %> 41 </div> 42 </div> 43 <div class="post_description"> 44 <div class="detail_description"> 45 <%= @post.description %> 46 </div> 47 </div> 48 <div class="post_bottom"> 49 <div class="post_name"> 50 投稿者: 51 <%= link_to @post.user.name, user_path(@post.user), class: :post__user %> 52 </div> 53 <div class="post_comment"> 54 "コメント" 55 </div> 56 </div> 57 <% if user_signed_in? %> 58 <%= form_with model: [@post, @comment], local: true do |f|%> 59 <div class="input"> 60 <%= f.text_field :content, id:"comment_content" %> 61 </div> 62 <div class="actions-comment"> 63 <%= f.submit "送信する", class: :comment__btn %> 64 </div> 65 <% end %> 66 <% end %> 67 <div class="comments_lists"> 68 <% @comments.each do |comment| %> 69 <div class="comments_list"> 70 <%= link_to "#{comment.user.name}:", user_path(comment.user), class: :comment_user %> 71 <%= comment.content%> 72 </div> 73 <% if user_signed_in? && current_user.id == comment.user_id %> 74 <%= link_to "削除", post_comment_path(comment.post.id, comment.id), method: :delete,class: :comment_delete %> 75 <% end %> 76 <% end %> 77 </div> 78 79 <% if user_signed_in? && current_user.id == @post.user_id %> 80 <div class="post__manage"> 81 <%= link_to "編集する", edit_post_path(@post.id), method: :get,class: :post__btn %> 82 <%= link_to "削除する", post_path(@post.id), method: :delete,class: :post__btn %> 83 </div> 84 <% end %> 85 </div> 86 </div> 87</main>
_like.html.erb
1<div class="like-hearts"> 2<%if user_signed_in? %> 3 <% if Like.find_by(user_id: current_user.id, post_id: @post.id) %> 4 <%= link_to post_like_path(@post,@post.likes), class: "like-link", method: :delete, remote: true do %> 5 <i class="fas fa-heart unlike-btn"></i> 6 <% end %> 7 <div class="count"><%= @post.likes.count %></div> 8 <% else %> 9 <%= link_to post_likes_path(@post,@post.likes), class: "like-link", method: :post, remote: true do %> 10 <i class="far fa-heart like-btn"></i> 11 <% end %> 12 <div class="count"><%= @post.likes.count %></div> 13 <% end %> 14 <% else %> 15 <i class="fas fa-grin-squint-tears unlike-btn"></i><p class="count"><%= post.likes.count %></p> 16<% end %> 17</div>
create.js.erb
1$("#like_heart_<%= @post.id %>").html("<%= j(render 'likes/like', locals: {post: @post}) %>");
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。