We are currently creating a favorite registration function.

The existing tables are as follows. (Since the text will be long, we will assume that the association is organized correctly.)

・ User table
・ Nutrition table * Ingredients
-Favorite table (intermediate table * columns are user_id and nutrition_id)

It is a function that users can register their favorite ingredients if they have them.

We have implemented a registration button and a release button in index/html.erb.
I can register, but when I press the release button, an error occurs.

[Displayed error]
ActionController :: UrlGenerationError in Nutritions # index
Showing /Users/shotaro_hosoda/projects/berries/app/views/nutritions/index.html.erb where line # 38 raised:

No route matches {: action =>"destroy",: controller =>"favorites",: nutrition_id =>27,: user_id =>3}, missing required keys: [: id]
Extracted source (around line # 38):

<% = link_to "register", user_favorites_path (user_id: current_user.id, nutrition_id: nutrition.id), method :: post%><% else%>↓ Error location -----<% = link_to "cancel", favorite_path (user_id: current_user.id, nutrition_id: nutrition.id), method :: delete%>-------------<% end%>

I think it's because I couldn't pass the primary key of the Favorite table, but I don't know how to define the primary key ...
I'm sorry that this was a fairly rudimentary question.
In this case, it will be the primary key of the intermediate table, but how do you define it and pass it as an argument? (What is written is the code that is trying to delete with find_by, passing the foreign keys user_id and nutrition_id)

----------- The following code ---------------

[View] index.html.erb
If it is registered, the release button is displayed. If it is not registered, the registration button is displayed.

<% = form_with (url: search_nutritions_path, local: true, method:: get, class: "search-form") do | form |%><% = form.text_field: keyword, placeholder: "Enter food name", class: "search-input"%><% = form.submit "Search", class: "search-btn"%><% end%><script src = "delete.js"></script>   Food list (per 100g)   <table border = "5" width = "80%" cellpadding = "20" bordercolor = "# 882d91" align = "left">  <tr>    <th width = "30%" height = "50">Food name</th>    <th width = "15%">Energy (kcal)</th>    <th width = "15%">Protein (g)</th>    <th width = "15%">Lipid (g)</th>    <th width = "15%">Carbohydrate (g)</th>    <th width = "15%">Potassium (mg)</th>    <th width = "15%">Calcium (mg)</th>    <th width = "15%">Iron (mg)</th><th width = "15%">Vitamin A (mg)</th>    <th width = "15%">Vitamin B1 (mg)</th>    <th width = "15%">Vitamin B2 (mg)</th>    <th width = "15%">Vitamin C (mg)</th>    <th width = "15%">Salt equivalent (g)</th>    <th width = "15%">Registered users</th>  </tr>  <% @ nutritions.each do | nutrition |%>    <tr height = "60">      <td><% = nutrition.ingredient%>                      <ul>            <li>              <% if current_user.already_favorited? (Nutrition)%>                <% = link_to'Release', nutrition_favorite_path (id: favorite.id, user_id: current_user.id, nutrition_id: nutrition_id), method:: delete%>              <% else%>                <% = link_to'Register', edit_nutrition_path (nutrition.id), method:: get%>              <% end%>              <% = link_to'edit', edit_nutrition_path (nutrition.id), method:: get%>              <% = link_to'Delete', nutrition_path (nutrition.id), method:: delete, data: {confirm:'Are you sure I want to delete? '}%>            </li>          </ul>                  </td>      <td><% = nutrition.calorie%></td>      <td><% = nutrition.protein%></td>      <td><% = nutrition.lipid%></td>      <td><% = nutrition.carbohydrate%></td>      <td><% = nutrition.potassium%></td>      <td><% = nutrition.calcium%></td>      <td><% = nutrition.iron%></td>      <td><% = nutrition.vitamin_a%></td>      <td><% = nutrition.vitamin_b1%></td>      <td><% = nutrition.vitamin_b2%></td>      <td><% = nutrition.vitamin_c%></td>      <td><% = nutrition.salt_equivalent%></td>      <td><% = nutrition.user.nickname%></td>    </tr>  <% end%></table>

[Routing] routes.rb

Rails.application.routes.draw do
  devise_for: users# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
  root to:'nutritions # index'
  resources: nutritionions do
    collection do
    member do
      post "add", to: "favorites # create"
    resource: favorites, only: [: destroy]
  resources: users do
    resources: favorites, only: [: show,: create]

[Controller] favorites_controller.rb

class FavoritesController


Thanks for your professor.

  • Answer # 1

    Since it is omitted, I am not sure what parameters should be passed in detail,missing required keys: [: id]It is said that you need to pass at least the id.

    I think it's because I couldn't pass the primary key of the Favorite table, but I don't know how to define the primary key.

    So I think that you can pass the id as follows.

    favorite_path (id: favorite.id, user_id: current_user.id, nutrition_id: nutrition.id)

    It seems that favorite is not defined in the actual code, but if it is only the operation, for examplefavorite.idFor the time being1It may be okay to enter an appropriate value.

    If you don't need id in path in the first place,resource: favorites, only: [: destroy]It may be a solution to define resources instead of resources.