非同期通信のいいね機能で出たエラーNo route matches
解決したいこと
ruby on railsで写真を投稿してクイズを出すサービスを開発しているのですが、写真のクイズの回答に対していいね機能を実装したいのですがそこでいいねを押しても表示が切り替わりません。ちなみにいいね解除の時は表示が切り替わります。
発生している問題・エラー
ActionView::Template::Error (No route matches {:action=>"destroy", :answer=>#<Answer id: 15, content: "かか", user_id: 3, photo_id: 4, created_at: "2021-04-26 17:20:32", updated_at: "2021-04-26 17:20:32">, :controller=>"likes"}, missing required keys: [:id]):
1: <% if current_user.best_answers?(answer) %>
2: <%= link_to 'いいね解除', like_path(answer: answer), method: :delete, class: 'answer-unlike__enable', remote: true %>
3: <% else %>
4: <%= link_to 'いいね', likes_path(answer: answer), method: :post, class: 'answer-like__enable', remote: true %>
5: <% end %>
app/views/likes/_like_button.html.erb:2:in `_app_views_likes__like_button_html_erb__2519850614983607465_47330826841800'
app/views/likes/create.js.erb:1:in `_app_views_likes_create_js_erb__1829228261995845859_47330833940560'
該当するソースコード
likes_controller.rb
class LikesController < ApplicationController
def create
@answer = Answer.find(params[:answer])
current_user.like(@answer)
end
def destroy
@answer = Answer.find(params[:answer])
current_user.unlike(@answer)
end
end
photos/show.html.erb
<p id="notice"><%= notice %></p>
<div class="row">
<div class="offset-md-3 col-md-5">
<p>
<%= image_tag @photo.image.url if @photo.image? %>
</p>
<p>
<strong>クイズの問題:</strong>
<%= @photo.content %>
</p>
<%= link_to "回答する", new_photo_answer_path(@photo) %>
<ul class="list-unstyled">
<% @answers.each do |answer| %>
<div class="media-body">
<div style="border: solid 1px #000; margin-bottom: 8px;">
<div class="user-name">
ユーザ名:<%= answer.user.name %> <div id="answer_<%= answer.id %>" style="float: right;"><%= render 'likes/like_button', answer: answer %></div>
</div>
<div>
<%= answer.content %>
</div>
</div>
</div>
<% end %>
</ul>
<%= paginate @answers %>
</div>
</div>
<%= link_to '戻る', photos_path %> |
<%= link_to '編集', edit_photo_path(@photo) %>
likes/_like_button.html.erb
<% if current_user.best_answers?(answer) %>
<%= link_to 'いいね解除', like_path(answer: answer), method: :delete, class: 'answer-unlike__enable', remote: true %>
<% else %>
<%= link_to 'いいね', likes_path(answer: answer), method: :post, class: 'answer-like__enable', remote: true %>
<% end %>
likes/create.js.erb
$("#answer_<%= @answer.id %>").html("<%= j(render 'likes/like_button', answer: @answer) %>")
likes/destroy.js.erb
$("#answer_<%= @answer.id %>").html("<%= j(render 'likes/like_button', answer: @answer) %>")
user.rb
class User < ApplicationRecord
validates :name, presence: true, length: { maximum: 50 }
validates :email, presence: true, length: { maximum: 255 },
format: { with: /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i },
uniqueness: { case_sensitive: false }
has_secure_password
has_many :photos, dependent: :destroy
has_many :answers
has_many :likes
has_many :best_answers, through: :likes, source: :answer
def like(answer)
self.likes.find_or_create_by(answer: answer)
end
def unlike(answer)
like = self.likes.find_by(answer: answer)
like.destroy if like
end
def best_answers?(answer)
self.best_answers.include?(answer)
end
end
自分で試したこと
ご教授お願いします。
0