#前提・実現したいこと
上記エラーを解決し、いいね機能を実装したい。
#エラー文
```ActionController::UrlGenerationError in Businesses#index`
Showing /Users/takayoshikanamoto/projects/C×S/app/views/likes/_like.html.erb where line #2 raised:
No route matches {:action=>"destroy", :business_id=>9, :company_id=>[14, 17, 15, 18], :controller=>"likes"}, missing required keys: [:id]
#関連コードソース ```ruby: #index.html.erb <%= render "shared/header" %> <div class="top_content"> <h1> 業務一覧 </h1> </div> <div class="middle_content"> <div class="company_information"> <% @business.each do |business|%> <%# <% link_to "#" do %> <ul class="divided_company_info"> <li class = "business_company_image"> <%= image_tag business.image, class:"business_img" %> </li> <li class = "business_company_name"> <%= %> </li> <li class="business_content"> <%= business.content%> </li> <li class="business_reward"> <%= business.reward%> </li> <div id="like-btn-<%= business.id %>"> <%= render 'likes/like', business: business, company: @company %> </div> </ul> <%# <% end %> <% end %> </div> </div> <div class="low_content"> <h1> 人気の企業一覧 </h1> ここにいいね件数の多い会社を出力する </div> <%= render "shared/footer" %>
ruby:
1#business.rb 2class Business < ApplicationRecord 3 4 belongs_to :company 5 belongs_to :student 6 has_one_attached :image 7 has_many :likes,dependent: :destroy 8 9 with_options presence: true do 10 validates :content 11 validates :department 12 validates :period 13 validates :reward 14 validates :number 15 validates :comment 16 validates :skill 17 end 18 19 def self.search(search) 20 return Business.all unless search 21 Business.where('content LIKE(?)', "%#{search}%") 22 end 23 24 def liked_by?(student) 25 likes.where(student_id: student.id).exists? 26 end 27 28end 29
ruby:
1#_like.html.erb 2<% if business.liked_by?(current_student) %> 3 <%= link_to(company_business_like_path(company.ids, business.id), method: :delete, remote: true, id: :"like-button-#{business.id}") do %> 4 <i class="fa-lg fas fa-heart icon-btn liked"></i> 5 <% end %> 6<% else %> 7 <%= link_to(company_business_likes_path(company.ids, business.id), method: :post, remote: true, id: :"like-button-#{business.id}") do %> 8 <i class="fa-lg fas fa-heart icon-btn not-like"></i> 9 <% end %> 10<% end %>
ruby: likes_controller.rb <% if business.liked_by?(current_student) %> <%= link_to(company_business_like_path(company.ids, business.id), method: :delete, remote: true, id: :"like-button-#{business.id}") do %> <i class="fa-lg fas fa-heart icon-btn liked"></i> <% end %> <% else %> <%= link_to(company_business_likes_path(company.ids, business.id), method: :post, remote: true, id: :"like-button-#{business.id}") do %> <i class="fa-lg fas fa-heart icon-btn not-like"></i> <% end %> <% end %>
ruby:
1routes.rb 2 3company_business_likes POST /companies/:company_id/businesses/:business_id/likes(.:format) likes#create 4company_business_like DELETE /companies/:company_id/businesses/:business_id/likes/:id(.:format) likes#destroy 5
#考察
No route matches {:action=>"destroy", :business_id=>9, :company_id=>[14, 17, 15, 18], :controller=>"likes"}, missing required keys: [:id]
とエラーで指摘されていることから、<%= link_to(company_business_like_path(company.ids, business.id), method: :delete, remote: true, id: :"like-button-#{business.id}") do %>
の記述(パス)が誤っていると考え、変数の中を確認、rails routes
でルーティングの確認をしたが解決の糸口を得られなかった。
#最後に
みなさんの知恵をお借りできたらと考えております。
お忙しいとは思いますが、何卒よろしくお願い致します。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/03/10 01:04
2021/03/11 12:19
2021/03/12 11:06
2021/03/12 11:17