简体   繁体   English

在 ruby​​ 应用程序的 api/v1 中为 POST 请求更新数组 [JSON]

[英]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.

相关问题 Ruby JSON API和Postman的发布请求 - Ruby JSON API and post request with Postman Api::V1::CompaniesController#index 缺少请求格式的模板:text/html - Api::V1::CompaniesController#index is missing a template for request formats: text/html Jbuilder对Ruby On Rails中的API使用POST请求 - Jbuilder use POST request to API in Ruby On Rails 在 Api::V1 中总是得到 JWTSessions::Errors::Unauthorized,在 Rails 和 jwt_sessions 中得到 nil JSON Token - Always get JWTSessions::Errors::Unauthorized in Api::V1, nil JSON Token in Rails and jwt_sessions ActionController::UrlGenerationError: 没有路由匹配 {:action=>"index", :controller=>"api/v1/topics", :format=>:json} - ActionController::UrlGenerationError: No route matches {:action=>"index", :controller=>"api/v1/topics", :format=>:json} 构建Json api时出错:没有路由与[GET]“ / v1 / posts匹配 - Error while building an Json api : No route matches [GET] "/v1/posts “未初始化的常量 Api::V1::ReservationOptionsController::ReservationOptions” - "uninitialized constant Api::V1::ReservationOptionsController::ReservationOptions" 没有路线与[GET]“ / api / v1 / users”匹配 - No route matches [GET] “/api/v1/users” 未初始化常量 API::V1::ApplicationSerializer - uninitialized constant API::V1::ApplicationSerializer API :: V1 :: Projects:Class的未定义方法`' - undefined method ` ' for API::V1::Projects:Class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM