How should I format a JSON POST request to my rails app?

My Rails app has a player class that works perfectly. Players can be created, deleted, and updated from my rails control panel without any issues. I would like a remote counterpart to be able to join in the fun by creating players using a JSON request. Following the advice of the auto generated Rails comments above my create method : `# POST /players.json` I have started sending requests to `localhost:3000/players.json` **The JSON** { "player": { "name": "test", "room_id": 0, "skin_id": 1, "head_id": 2, "torso_id": 3, "legs_id": 4, "x_position": 5, "y_position": 6, "direction": 7, "action": "", "gender": "f" } } However, I am running into this error message: `ActionController::ParameterMissing in PlayersController#create param not found: player` So I guess my question is: **How should I structure the JSON I am sending?** **Additional info:** - Ruby Version: 2.0 - Rails Version: 4.0 - I have tried sending my requests using [Postman][1] **Update - Player Params** Here is the player params method from my controller (as requested): def player_params params.require(:player).permit(:name, :room_id, :skin_id, :head_id, :torso_id, :legs_id, :x_position, :y_position, :direction, :action, :gender) end **Update 2 - Player controller** class PlayersController < ApplicationController before_action :set_player, only: [:show, :edit, :update, :destroy] skip_before_filter :verify_authenticity_token, :if => Proc.new { |c| c.request.format == 'application/json' } # GET /players # GET /players.json def index @players = Player.all end # GET /players/1 # GET /players/1.json def show end # GET /players/new def new @player = Player.new end # GET /players/1/edit def edit @rooms = Room.all.map { |room| [room.name, room.id] } end # POST /players # POST /players.json def create @player = Player.new(player_params) respond_to do |format| if @player.save format.html { redirect_to @player, notice: 'Player was successfully created.' } format.json { render action: 'show', status: :created, location: @player } else format.html { render action: 'new' } format.json { render json: @player.errors, status: :unprocessable_entity } end end end # PATCH/PUT /players/1 # PATCH/PUT /players/1.json def update respond_to do |format| if @player.update(player_params) format.html { redirect_to @player, notice: 'Player was successfully updated.' } format.json { head :no_content } else format.html { render action: 'edit' } format.json { render json: @player.errors, status: :unprocessable_entity } end end end # DELETE /players/1 # DELETE /players/1.json def destroy @player.destroy respond_to do |format| format.html { redirect_to players_url } format.json { head :no_content } end end def manage_furni @player = Player.find(params[:id]) @furni = Furni.all end def add_furni player = Player.find(params[:id]) player.furnis << Furni.find(params[:furni]) redirect_to manage_furni_path(player) end def remove_furni player = Player.find(params[:id]) item = InventoryItem.find(params[:item]) player.inventory_items.delete(item) redirect_to manage_furni_path(player) end private # Use callbacks to share common setup or constraints between actions. def set_player @player = Player.find(params[:id]) end # Never trust parameters from the scary internet, only allow the white list through. def player_params params.require(:player).permit(:name, :room_id, :skin_id, :head_id, :torso_id, :legs_id, :x_position, :y_position, :direction, :action, :gender) end end **Update 3: logs** ( "Processing by PlayersController#create as JSON", "Completed 400 Bad Request in 31ms", "ActionController::ParameterMissing (param not found: player):", "app/controllers/players_controller.rb:103:in `player_params'", "app/controllers/players_controller.rb:40:in `create'", "Rendered /Users/drewwyatt/.rvm/gems/ruby-2.0.0-p247/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/_source.erb (0.5ms)", "Rendered /Users/drewwyatt/.rvm/gems/ruby-2.0.0-p247/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/_trace.erb (0.9ms)", "Rendered /Users/drewwyatt/.rvm/gems/ruby-2.0.0-p247/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (0.8ms)", "Rendered /Users/drewwyatt/.rvm/gems/ruby-2.0.0-p247/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (16.3ms)" ){ } [1]: http://getpostman.com/
Did you write a method to require the player params in your controller? github.com/rails/strong_parameters

以上就是How should I format a JSON POST request to my rails app?的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » JSON 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准

联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏