我尝试在rails调查应用上创建基本主干,以了解嵌套模型并与服务器进行交互。在用户选择后,我真的在努力创建选择。
这是一个curl请求,从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可能会以null形式出现?知道为什么可能或者我需要做什么才能在卷曲中创建上面的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
答案 0 :(得分: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
)