2014-06-29 32 views
0

我正在嘗試在軌道調查應用上創建一個基本骨幹,以瞭解嵌套模型並與服務器進行交互。我很努力地在用戶選擇它們之後創建選項。Backbone Post JSON null?

這是工作/最好的捲曲請求,應該從一個JSON/POST的角度看,:

curl -XPOST -H "Content-Type: application/json" http://localhost:3000/choices -d '{"answer_id":"3", "appuser_id":"1", "question_id":"2"}' 

下面是我創造它:

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

而選擇模型:

class SurveyMe.Models.Choice extends Backbone.RelationalModel 

    urlRoot: '/choices' 

認爲的JSON可能合作明白爲空?任何想法爲什麼這可能是或我需要做什麼來創建上面的curl JSON?

接頭:

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:2 
Content-Type:application/json 
Cookie:request_method=GET; user=6c7f11ad2bd9d825cc89f77b8f24d008; _survey_me_session=ZTh0MHVsUTd0amx6YnpPMEs3NlE4OWtkM0dDdjEzNndzUFc0OFdMS1NSWFZibHJFRXpDdGJRdVZETW9qSUQ3dUNVZjF1Q2QrdndKWVBWN1Z0M2VwRnZRSU95a3ZrU1JPSjArMjgxdmFMT3FXSk43L21kOTQxdVJJejZjb1p5V21EdUIwenlKbjc1dFlCbUtXd3B6TTNKRjhqSGxvd0FobC9Nblo4Y0xRL0VXMjhqb2xrb3B1Ryt3UnRERUFDRGhnLS1NWGc4eGlKSzRTZm96azJqL0ZhbjhRPT0%3D--f00afae157b22e2f4f5a052e415603070b476f2e 
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 
{} 
No Properties 
Response Headersview source 
Connection:Keep-Alive 
Content-Length:16491 
Content-Type:text/html; charset=utf-8 
Date:Sun, 29 Jun 2014 03:22:21 GMT 
Server:WEBrick/1.3.1 (Ruby/2.0.0/2013-05-14) 
X-Request-Id:4276e2be-224c-4829-87ce-0a40e960d5dd 
X-Runtime:0.105006 

預覽:

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: 

{"choice"=>{}} 
Toggle session dump 
Toggle env dump 
Response 

Headers: 

None 

回答

2

骨幹模型屬性和JavaScript對象屬性在模型上是完全不同的東西。當你這樣說:

choice.appuser = Cookie.get('user') 

你設置appuser財產,而不是appuser屬性。當你對它們進行CRUD操作時,骨幹模型只知道屬性。你想用set設置屬性,使save會知道他們:

choice = new SurveyMe.Models.Choice 
choice.set('appuser', Cookie.get('user')) 
choice.set('question', 3) 
choice.set('answer, 4) 
choice.save() 

或一次將它們都:

choice = new SurveyMe.Models.Choice 
choice.set(
    appuser: Cookie.get('user') 
    question: 3 
    answer: 4 
) 
choice.save() 

,甚至他們交給save

choice = new SurveyMe.Models.Choice 
choice.save(
    appuser: Cookie.get('user') 
    question: 3 
    answer: 4 
) 
+0

啊你釘了它。 –