[英]Update an Array[JSON] for a POST request in api/v1 of a ruby application
I'm leraning Ruby and I'm trying to create an API to update all the players with a single POST request.我正在学习 Ruby,并且正在尝试创建一个 API,以通过一个 POST 请求来更新所有玩家。 I have to update all the players which have the "ids" on the request.
我必须更新所有在请求中具有“id”的玩家。 The post request is given in Array[JSON] form, so I have to compare all the ids for every JSON with the players in the database and I have to update those who have the id specified.
发布请求以 Array[JSON] 形式给出,因此我必须将每个 JSON 的所有 id 与数据库中的玩家进行比较,并且我必须更新那些指定了 id 的玩家。 Here the code of the post request:
这里是post请求的代码:
{
"players": [
{
"player_id": 1,
"shirt_number": "25",
"speed": "47",
"agility": "80",
"shot": "62",
"step": "24",
"technique": "70",
"contrasts": "59",
"resistance": "65",
"parades": "25",
"aggressiveness": "60",
"coldness": "50"
},
{
"player_id": 2,
"shirt_number": "11",
"speed": “52",
"agility": “78",
"shot": “65",
"step": “23",
"technique": “60",
"contrasts": “19",
"resistance": “44",
"parades": "25",
"aggressiveness": "60",
"coldness": "50"
}
]
}
Here there is the code and the parameters:下面是代码和参数:
desc 'Update a team'
params do
requires :players, type: Array[JSON] do
requires :id, type: Integer, desc: "Player ID"
requires :shirt_number, type: String, desc: "Shirt Number"
requires :speed, type: String, desc: "Speed"
requires :agility, type: String, desc: "Agility"
requires :shot, type: String, desc: "Shot"
requires :step, type: String, desc: "Step"
requires :technique, type: String, desc: "Technique"
requires :constrasts, type: String, desc: "Constrasts"
requires :resistance, type: String, desc: "Resistance"
requires :parades, type: String, desc: "Parades"
requires :aggressiveness, type: String, desc: "Aggressiveness"
requires :coldness, type: String, desc: "Coldness"
end
end
post 'update_team' do
params[:players].each do |player|
p = Player.find_by(id: params[:id])
p.update(player)
end
end
But it doesn't work!但它不起作用! I didn't find anything about that.
我没有找到任何关于那个的东西。 Someone can help me?
有人可以帮助我吗? Thank you very much in advance!
非常感谢您提前!
p = Player.find_by(id: params[:id])
-> From what you've posted, there isn't an id
parameter and if there was, that would be referencing the same parameter each time through your loop. p = Player.find_by(id: params[:id])
-> 从您发布的内容来看,没有id
参数,如果有,则每次循环都会引用相同的参数。 I think what you want is p = Player.find_by(id: player['player_id'])
我想你想要的是
p = Player.find_by(id: player['player_id'])
Also keep in mind that Player.find_by(id: some_value)
is looking at the database ID, which I imagine might be different from the player_id
.还要记住
Player.find_by(id: some_value)
正在查看数据库 ID,我想这可能与player_id
不同。 If this is the case, then player_id
should probably be a different column in your players table and you would instead do .find_by(player_id: player['player_id'])
如果是这种情况,那么
player_id
可能应该是玩家表中的不同列,而您应该执行.find_by(player_id: player['player_id'])
Additionally, if you are just updating the record in place, you should use update!
此外,如果您只是在原地更新记录,则应该使用
update!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.