简体   繁体   中英

How to parse POST request in JSON format in rails

I'm trying to send a POST request in JSON via curl in ruby on rails. I would like to know how can I parse this request in a controller and save it in database. I've created controller and model for a player.

Below is the curl command -

curl -i -X POST -H "Content-Type: application/json" "localhost:3000/app/v2/player" -d '{
"url": "app/v2/players.json",
"para": {
    "player": {
        "player_id": 51275,
        "email": "test@gmail.com",
        "total_won": "1890000.00",
        "pr_costs": "29.00",
        "payment_method": "VISA",
        "games": [
            {
                "name": "AvsB",
                "matches": 1,
                "value": 1100,
                "category": "Cricket",
                "subcategory": "Test",
                "tags": [
                    "Aus",
                    "Melbourne"
                ],
                "game_id": 12                   
            },
            {
                "name": "CvsD",
                "matches": 1,
                "value": 790,
                "category": "Cricket",
                "subcategory": "T20",
                "tags": [
                    "T20",
                    "20over",
                    "worldcup"
                ],
                "game_id": 7
            }
        ]
    }
}
}'  

Below is my player controller-

module App
module V2
    class PlayersController < ApplicationController

  def create

    @player = Player.new(params[[:para][:player][:player_id]].first)
    if @player.save
        render json: @player, status: 201, location: @player

    else
        render json: @player.errors, status: 422
    end
  end


private  

def player_params
  #params.require(:player).permit(:url, :player_id, :email, :total_won, :pr_costs, :payment_method)
  params.fetch(:url,:para, {}).permit(:player_id, :email, :total_won, :pr_costs, :payment_method)

end
    end
end
end

Below is my player model -

class Player < ActiveRecord::Base
has_many :games, dependent: :destroy
validates :player_id, presence: true
attr_accessor :url,:para


end

Currently I'm getting below error while initiating the curl command -

 TypeError (no implicit conversion of Symbol into Integer):
 app/controllers/app/v2/players_controller.rb:21:in `[]'
 app/controllers/app/v2/players_controller.rb:21:in `create'

Any pointers on how I can read JSON data and create records for players in create function and related games would be appreciated. I've not created the model for games but it'll be similar in structure to JSON data. Also one player has many games and a game will belong to a Player.

Thanks in advance!

This is badly structured.

[[:para][:player][:player_id]]

It basically means (in part) there's an array that contains the player symbole

[:player]

and you want to get the ':player_id(th)' entry of that array or string... which is where you're getting the error ("How do I convert a symbol into an integer for the array?")

[:player][:player_id]

you probably want...

params[:para][:player][:player_id]

There's no 'first' because there's no array in the :player_id entry.

As it always happens, I figured it out myself. Below is what I used for player_params function. There could be other ways as well, but this certainly worked for me.

params.require(:player).permit(:url,:para =>[:player =>[:player_id, :email, :total_won, :pr_costs, :payment_method,:games =>[:name, :matches, :value, :category, :subcategory, :game_id ]]])

@SteveTurczyn thanks for pointing me in the right direction

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM