[英]Add information to DB from the controller on rails
I'm trying to add the user id to 2 columns in the db after a user successfully submits a form. 我试图在用户成功提交表单后将用户ID添加到数据库的2列中。 The commands run fine in the console but wont work in the controller.
这些命令在控制台中运行正常,但在控制器中不起作用。 I haven't added info to the db straight from the controller before so I'm thinking that I'm doing something wrong.
之前我还没有直接从控制器向数据库添加信息,所以我认为我做错了什么。
Here is the create method 这是创建方法
def create
@game = Game.friendly.find(params[:game_id])
@game_category = Game.friendly.find(@game.id).game_categories.new(game_category_params)
if current_user.mod_of_game? params[:game_id] && @game_category.save
Game.find(@game.id).game_categories.find(game_category.id).update(submitted_by: current_user.id)
Game.find(@game.id).game_categories.find(game_category.id).update(approved_by: current_user.id)
flash[:info] = "Game category added succesfully!"
redirect_to @game
else
render 'new'
end
end
These 2 lines are supposed to add the user id to the submitted_by and approved_by columns but they don't. 这两行应该将用户ID添加到Submitted_by和roved_by列中,但没有添加。 I don't get any error messages, they simply just don't add anything to those columns
我没有收到任何错误消息,他们只是不向这些列添加任何内容
Game.find(@game.id).game_categories.find(game_category.id).update(submitted_by: current_user.id)
Game.find(@game.id).game_categories.find(game_category.id).update(approved_by: current_user.id)
If I replace the lines with coding that works in the console to see if its a variable or something thats not right it still doesn't work 如果我将这些行替换为在控制台中可用的编码,以查看其是否为变量或不正确的内容,则仍然无法正常工作
Game.find(12).game_categories.find(55).update(submitted_by: 1)
Game.find(12).game_categories.find(55).update(approved_by: 1)
I'm building an app to learn rails and I guess this is something I just don't know. 我正在构建一个用于学习Rails的应用程序,我想这只是我不知道的事情。
Can anyone enlighten me on what I'm doing wrong? 有人能启发我做错什么吗?
Update: 更新:
Ok it is now giving me an error - Couldn't find GameCategory without an ID 好的,现在给我一个错误-没有ID找不到GameCategory
So the @game_category.id isn't working? 所以@ game_category.id无法正常工作?
这是您错过@
的查询中的一个小错字。
Game.find(@game.id).game_categories.find(@game_category.id).update(submitted_by: current_user.id)
After playing around tweaking it here and there it turns out to be this line 经过四处调整之后,原来是这条线
if current_user.mod_of_game? params[:game_id] && @game_category.save
when changed to this it works 当更改为此时
if @game_category.save && current_user.mod_of_game? @game
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.