问题
In my specs, when I run the POST request below, everything works fine.
before do
request_payload = {
player: {
first_name: "Joe",
last_name: "Carradine",
team_id: "1"
}
}
post :create, request_payload
end
But when I run a spec for PUT:
before do
request_payload = {
player: {
first_name: "Buck",
last_name: "Carradine",
team_id: "1"
}
}
put :update, id: 3, request_payload
end
I get an error like this:
[filename]_spec.rb:139: syntax error, unexpected '\n', expecting tASSOC (SyntaxError)
[filename]_spec.rb:198: syntax error, unexpected $end, expecting keyword_end
Any ideas? Is there a different syntax for PUT I'm not aware of?
回答1:
To fix the syntax error, use put :update, { id: 3 }, request_payload
, not put :update, id: 3, request_payload
. Ruby only supports "bare" (e.g. curly-braceless) hashes as the last argument to the method, so id: 3
cannot appear in the middle of an argument list without being wrapped in curly braces.
来源:https://stackoverflow.com/questions/13024146/receiving-error-in-rspec-for-put-but-not-post