Backbone Rails Post 400 Bad Request

时间:2014-06-29 20:20:18

标签: ruby-on-rails json backbone.js

我正在创建一个带有Rails后端和Backbone前端的基本调查应用,以帮助学习骨干。但是我遇到了一些问题 - 在尝试发布到服务器时,我一直收到400 Bad Request。一如往常,任何帮助都非常感谢!

每次选择后,我都会发送一个POST来创建选择。这是发送的JSON:

{"appuser"=>3, "question"=>3, "answer"=>4, "choice"=>{}}

似乎JSON可能应该是{"appuser"=>3, "question"=>3, "answer"=>4},但我不完全确定如何从骨干角度调整它。我想我不确定"choice"=>{}正在做什么。

这是ChoiceController.rb:

class ChoicesController < ApplicationController
  before_action :set_choice, only: [:show, :edit, :update, :destroy]
  skip_before_action :verify_authenticity_token

  def index
    @question = Question.find(params[:question_id])
  end

  # POST /choices
  # POST /choices.json
  def create
    @choice = Choice.new(choice_params)

    respond_to do |format| 
      if @choice.save
        format.html { redirect_to question_choices_path, notice: 'Choice was successfully created.' }
        format.json { redirect_to question_choices_path, status: :created, location: @choice }
      else
        format.html { render action: 'new' }
        format.json { render json: @choice.errors, status: :unprocessable_entity }
      end
    end
  end


  # DELETE /choices/1
  # DELETE /choices/1.json
  def destroy
    @choice.destroy
    respond_to do |format|
      format.html { redirect_to choices_url }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_choice
      @choice = Choice.find(params[:id])
    end

    def set_question
      @question = Question.find(params[:choice][:question_id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def choice_params
      params.require(:choice).permit(:appuser_id, :answer_id, :question_id)
    end
end

以下是我通过Backbone创建POST的方法:

events:
      'click #answer': 'updateQuestion'
      'click #back': 'back'

  updateQuestion: ->
    if @questionNumber < @questionLimit
      @questionNumber += 1
      $("#container").html(@render().el)
      choice = new SurveyMe.Models.Choice
      choice.save(
        appuser: parseInt(Cookie.get('survey_user_id'))
        question: 3
        answer: 4
      )
    else
      Backbone.history.navigate("surveys",trigger: true)

来自控制台的网络预览:

ActionController::ParameterMissing in ChoicesController#create
param not found: choice

Extracted source (around line #48):
4647484950          
    # Never trust parameters from the scary internet, only allow the white list through.
    def choice_params
      params.require(:choice).permit(:appuser_id, :answer_id, :question_id)
    end
end
Rails.root: C:/Users/thammond/Documents/GitHub/Survey.me

Application Trace | Framework Trace | Full Trace
app/controllers/choices_controller.rb:48:in `choice_params'app/controllers/choices_controller.rb:12:in `create'
Request

Parameters:

{"appuser"=>3, "question"=>3, "answer"=>4, "choice"=>{}}
Toggle session dump
Toggle env dump
Response

Headers:

None

接头:

Remote Address:127.0.0.1:3000
Request URL:http://localhost:3000/choices
Request Method:POST
Status Code:400 Bad Request
Request Headersview source
Accept:application/json, text/javascript, */*; q=0.01
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Content-Length:37
Content-Type:application/json
Cookie:request_method=GET; survey_user_id=3; survey_user_token=74e450e78e32c5587e55e38b5811ad31; _survey_me_session=Znd4NTRqR1BnNUVkNWwzTDhQUW1oWmlTSmQrOG81cXVvb21sNXNTK3Y3TDRqSkZTcFNDTzFlVExrckdQaS9mSGl6cUZQRDJSM1E4RzI4cldJUEFYZ1NDVktNRVZaMkVwamRhMXNTVXFjY0Z5MXp5MGhDb2IzOEdRcXhsRy9JbThxZi94ZkVjVFFXTGhpeCtEQU8vZHhmdmk0TS95NlEzVndKamtSZ1M5blQ3NDlJWVVuK0lHMURBeFJVL0xTamNpLS14MzZZb3dZcUZGQWw1SE80SU9IRTlnPT0%3D--062c1d715fd4908f9722954ec037752387499786
Host:localhost:3000
Origin:http://localhost:3000
Referer:http://localhost:3000/surveys/1
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36
X-CSRF-Token:yJTVE9Ce+a6uIfHk7QmXZsRtg4i7kAoFKjGzXs1Ileg=
X-Requested-With:XMLHttpRequest
Request Payloadview source
{appuser:3, question:3, answer:4}
answer: 4
appuser: 3
question: 3
Response Headersview source
Connection:Keep-Alive
Content-Length:16575
Content-Type:text/html; charset=utf-8
Date:Sun, 29 Jun 2014 20:07:59 GMT
Server:WEBrick/1.3.1 (Ruby/2.0.0/2013-05-14)
X-Request-Id:4b501d74-00b8-40ec-8f1b-325930920c66
X-Runtime:0.219012

发布的骨干保存方法:

 // Set a hash of model attributes, and sync the model to the server.
    // If the server returns an attributes hash that differs, the model's
    // state will be `set` again.
    save: function(key, val, options) {
      var attrs, method, xhr, attributes = this.attributes;

      // Handle both `"key", value` and `{key: value}` -style arguments.
      if (key == null || typeof key === 'object') {
        attrs = key;
        options = val;
      } else {
        (attrs = {})[key] = val;
      }

      options = _.extend({validate: true}, options);

      // If we're not waiting and attributes exist, save acts as
      // `set(attr).save(null, opts)` with validation. Otherwise, check if
      // the model will be valid when the attributes, if any, are set.
      if (attrs && !options.wait) {
        if (!this.set(attrs, options)) return false;
      } else {
        if (!this._validate(attrs, options)) return false;
      }

      // Set temporary attributes if `{wait: true}`.
      if (attrs && options.wait) {
        this.attributes = _.extend({}, attributes, attrs);
      }

      // After a successful server-side save, the client is (optionally)
      // updated with the server-side state.
      if (options.parse === void 0) options.parse = true;
      var model = this;
      var success = options.success;
      options.success = function(resp) {
        // Ensure attributes are restored during synchronous saves.
        model.attributes = attributes;
        var serverAttrs = model.parse(resp, options);
        if (options.wait) serverAttrs = _.extend(attrs || {}, serverAttrs);
        if (_.isObject(serverAttrs) && !model.set(serverAttrs, options)) {
          return false;
        }
        if (success) success(model, resp, options);
        model.trigger('sync', model, resp, options);
      };
      wrapError(this, options);

      method = this.isNew() ? 'create' : (options.patch ? 'patch' : 'update');
      if (method === 'patch') options.attrs = attrs;
      xhr = this.sync(method, this, options);

      // Restore attributes.
      if (attrs && options.wait) this.attributes = attributes;

      return xhr;
    },

1 个答案:

答案 0 :(得分:1)

你可以尝试一下,“强制”使用选择键吗?

  choice.save(
    choice:
       appuser: parseInt(Cookie.get('survey_user_id'))
       question: 3
       answer: 4
  )

也许它会起作用......由于参数很强,你的哈希的第四部分无论如何都不会被使用。