2016-02-16 17 views
0

我試圖通過捲曲在JSON發送POST請求在Ruby on Rails的JSON格式的POST請求。我想知道如何在控制器中解析此請求並將其保存在數據庫中。我爲玩家創建了控制器和模型。如何分析在軌

下面是curl命令 -

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": "[email protected]", 
     "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 
      } 
     ] 
    } 
} 
}' 

下面是我的球員控制器 -

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 

下面是我的球員的典範 -

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


end 

目前我得到以下錯誤同時啓動捲曲命令 -

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' 

我如何可以讀取JSON數據,並創建記錄玩家在創建功能以及相關的遊戲任何指針將不勝感激。我還沒有創建遊戲模型,但它的結構與JSON數據類似。另外一個玩家有很多遊戲,一個遊戲將屬於一個玩家。

在此先感謝!

+1

爲什麼你有一個額外的'[]'的'params' 嘗試'PARAMS [:對] [:玩家]' – gmaliar

回答

0

這是嚴重的結構。

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

它主要是指(部分)有包含玩家symbole

[:player] 

數組,你想獲得「:player_id(TH)」該數組或字符串進入.. 。這是你在哪裏得到的錯誤(「我如何轉換的象徵到一個整數的陣列?」)

[:player][:player_id] 

你可能想...

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

有沒有「第一」,因爲有一個在:player_id項沒有數組。

+0

如果我使用'@player = Player.new(params [:para] [:player] [:player_id])',我得到一個錯誤,說'ArgumentError(當分配屬性時,你必須傳遞一個散列作爲參數。): app/controllers/app/v2/players_controller.rb:21:在'create''你能告訴我在這裏做錯了嗎? – roller

+0

drop'[:player_id]'因爲這只是一個元素。如果你放棄它,你現在傳遞一個散列。你可能會得到另一個錯誤,因爲你傳遞一點用處都沒有額外的參數,但我們已經取得了進步 – SteveTurczyn

+0

或者做一個全面的'player_params'方法(恢復'require'),然後引用'player_params'代替直接參數。 – SteveTurczyn

0

因爲它總是會發生的,我理解了它自己。以下是我用於player_params函數的內容。也可以有其他的方式,但這對我來說確實有效。

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感謝我指出了正確的方向