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
get'search'
end
member do
post "add", to: "favorites # create"
end
resource: favorites, only: [: destroy]
end
resources: users do
resources: favorites, only: [: show,: create]
end
end
[Controller] favorites_controller.rb
class FavoritesController
――――――――――――――――――――――――――――――――――――――――――――――
Thanks for your professor.
-
Answer # 1
Related articles
- ruby - the error in rails g migration cannot be resolved
- ruby on rails - unknown action error when trying to post a comment
- ruby - rails cannot be started in the production environment when building an ec2 environment
- ruby on rails - you cannot change the keychain after downloading the pem file when creating an ec2 instance the matter that come
- ruby on rails - when calling the create action, the name attribute changes depending on whether it is indexhtmlerb or newhtmlerb
- ruby on rails - data cannot be saved in the intermediate table (parameters cannot be passed)
- ruby - [rails] the cart function cannot get the cart id associated with the user
- ruby - data is not updated by update action rails
- ruby on rails - i cannot save a comment on the comment posting page
- ruby on rails - webpacker cannot be compiled
- ruby on rails runtime error cannot be resolved
- ruby on rails 5 - actioncontroller :: invalidauthenticitytoken cannot be resolved
- ruby on rails - syntax error cannot be resolved
- ruby on rails - cannot start unicorn cannot load `railsconfigactive_storageservice`
- ruby on rails - not saved by create action
- ruby on rails - cannot save nested child model
- ruby - destroy action does not respond
- ruby on rails - i want to jump to the action of another controller with render
- ruby on rails - image cannot be saved (activestorage installed) null in image of table
- python - you may need to restart the kernel to use updated packages error
- dart - flutter: the instance member'stars' can't be accessed in an initializer error
- php - coincheck api authentication doesn't work
- php - i would like to introduce the coincheck api so that i can make payments with bitcoin on my ec site
- [php] i want to get account information using coincheck api
- the emulator process for avd pixel_2_api_29 was killed occurred when the android studio emulator was started, so i would like to
- javascript - how to check if an element exists in puppeteer
- sh - 'apt-get' is not recognized as an internal or external command, operable program or batch file
- i want to check the type of a shell script variable
- i want to call a child component method from a parent in vuejs
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.So I think that you can pass the id as follows.
It seems that favorite is not defined in the actual code, but if it is only the operation, for example
favorite.id
For the time being1
It 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.