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)
If there is a food that the user likes, I would like to save the current_user.id and the id of the corresponding nutrition in the Favorite table by pressing the registration button, but ... Although the parameters can be passed, the "Nutrition ID" is I'm in a state where I've been told "No!" ...
[Displayed error]
ActiveRecord :: RecordNotFound in FavoritesController # create
Couldn't find Nutrition without an ID
----------- The following code ---------------
[View] index.html.erb
Omission
<% if! Favorite.exists? (User_id: current_user.id, nutrition_id: nutrition.id)%>
<% = link_to "favorites", user_favorites_path (user_id: current_user.id, nutrition_id: nutrition.id), method :: post%>
<% else%>
<% = link_to "Unfavorite", favorite_path (current_user.id, nutrition.id), method:: delete%>
<% end%>
Omission
[Routing] routes.rb
Omission
resources: users do
resources: favorites, only: [: show,: create,: destroy] ← The action used this time is create
end
Omission
[Controller] favorites_controller.rb
Omission
def create
@user_id = session [: id]
@nutrition_id = Nutrition.find (params [: id]). id ← Error occurred here
@favorite = Favorite.new (nutrition_id: @nutrition_id, user_id: @user_id)
if @ favorite.save
redirect_to user_favorite_path (session [: id])
end
end
Omission
def favorite_params
params.require (: favorite) .permit (: user_id,: nutrition_id)
end
end
――――――――――――――――――――――――――――――――――――――――――――――
I have parameters, but why can't I pass them? (From the error below)
Parameters:
{"_method" =>"post", "authenticity_token" =>"MajNmuaXydwLwBBdnogZfKHJvWddHFCsD9W4InLKU3g9omXjGdPjsl0Bqq8hOZsNC9K/XkyLOH7KjOkzymH/2w ==", "
Thanks for your professor.
-
Answer # 1
Related articles
- ruby on rails - image cannot be saved (activestorage installed) null in image of table
- ruby on rails - the image cannot be saved in s3 and cannot be displayed in the view aws :: sigv4 :: errors :: missingcredentials
- ruby on rails 5 - couldn't find user with'id'= cannot be resolved
- ruby on rails - i cannot save a comment on the comment posting page
- ruby - cannot be saved due to validation of the mating table of the intermediate table
- ruby - rails cannot be started in the production environment when building an ec2 environment
- ruby on rails - rails associated model is not saved in db
- ruby - not saved with rails new save method
- ruby on rails - you cannot change the keychain after downloading the pem file when creating an ec2 instance the matter that come
- ruby - [rails] the cart function cannot get the cart id associated with the user
- ruby on rails - cannot pass id to destroy action of intermediate table
- ruby on rails - webpacker cannot be compiled
- ruby on rails - i implemented it so that images can be saved in s3 of aws, but the view can no longer be displayed
- ruby on rails runtime error cannot be resolved
- ruby on rails 5 - some items are not saved in db when posting from rails form
- ruby - i created an intermediate table, but no data is saved in the intermediate table
- ruby on rails 5 - actioncontroller :: invalidauthenticitytoken cannot be resolved
- ruby on rails - cannot save nested child model
- ruby on rails - saved description in nested controller
- 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
I am
nutrition_id
However, I am referring to it.params [: id]
That's why.@nutrition_id = params [: nutrition_id]
To.You don't have to "finde with id and take that id".