[英]JSON parsing throws error for nil while payload has no null values
I see this rather strange issue building my first rails 4 demo app, I have the POST request working through angular, however parsing it on the controller throws the error 我看到了构建我的第一个Rails 4演示应用程序时遇到的一个相当奇怪的问题,我的POST请求通过angular工作,但是在控制器上解析它会引发错误
Started POST "/home/contact" for 127.0.0.1 at 2015-07-21 18:40:21 +0530
Processing by HomeController#contact as JSON
Parameters: {"name"=>"Gabbar Singh", "email"=>"gabbar@rampur.in", "phone"=>"9820420420", "message"=>"kitne aadmi thein", "captcha"=>"03AHJ_VuvKsc5W9IrGKKnitvFp8niuxL2cnpIpJ2WgGXPkQFYVXBe4KKwfcLtVhrx3Juos-R36WcYGScXbJgA9ZLjoznN7ABvjnHlNQ5r5z25-jCs7BKJBf14ITvCqVqYRoU4Je2c7EJXa7K3IjPmNXeWvUosYbLsoGAtnlH17ScIM-MLrm9iDBAJqTO0xiOY4yYI7rYSgfcXcZg7DGLioni8XEelTqgjrlEoK21ORwstV8i90zRfqTmQExs8TjcYFZKzTD4oHqBbsXQCA4GMvrMRiWK5OmzXvauopthhijUDCDJqtg-cWGGuMCOb6dPNkqXW7oEMVCBo-U_e0Nw1dpXh0Goui8pgqvseadEG120d3kVZcke8WcmnIKdi2_VkUxhppOYnXjnAh"}
Completed 500 Internal Server Error in 2ms
TypeError (no implicit conversion of nil into String):
app/controllers/home_controller.rb:9:in `contact'
the ContactRequest
object is as follows ContactRequest
对象如下
class ContactRequest
include ActiveModel::Model
attr_accessor :name, :email, :phone, :message, :captcha
validates :name, presence: true,length: {in:2..255}
validates :email, presence: true, length: {in:6..255}
validates :message, presence: true
validates :phone, presence: true
validates :captcha, presence: true
end
And the angular snippet posting this data is 而发布此数据的角度片段是
var post_data = { //prepare payload for request
'name':$scope.contact.name,
'email':$scope.contact.email,
'phone':$scope.contact.phone,
'message':$scope.contact.message,
'captcha':$scope.response //send g-captcha-reponse to our server
}
$http.post(
'home/contact',post_data
).success(function(response){
console.log(response);
//flush form data
$scope.contact = {};
if(response.error === 0){
$scope.messages = "Thanks!" + $scope.contact.name + "for your request! We will get back to you shortly"
}else{
$scope.messages = "We are sorry but your request was not processed, please try again."
}
})
Controller where I get error 出现错误的控制器
class HomeController < ApplicationController
wrap_parameters false
def index
end
def contact
@contactReq = HomeHelper::ContactRequest.new(JSON.parse(params[:json]))
logger.debug "ContactRequest: #{@contactReq.attributes.inspect}"
end
end
I don't any nil or blank field for that matter in the logs nor in the form POST request payload in firebug 我在日志中也没有任何零或空白字段,在Firebug中也没有POST请求有效载荷的形式
{
"name":"Gabbar Singh",
"email":"gabbar@rampur.in",
"phone":"9820420420",
"message":"kitne aadmi thein",
"captcha":"03AHJ_VuvKsc5W9IrGKKnitvFp8niuxL2cnpIpJ2WgGXPkQFYVXBe4KKwfcLtVhrx3Juos-R36WcYGScXbJgA9ZL
joznN7ABvjnHlNQ5r5z25-jCs7BKJBf14ITvCqVqYRoU4Je2c7EJXa7K3IjPmNXeWvUosYbLsoGAtnlH17ScIM-MLrm9iDBAJqTO
0xiOY4yYI7rYSgfcXcZg7DGLioni8XEelTqgjrlEoK21ORwstV8i90zRfqTmQExs8TjcYFZKzTD4oHqBbsXQCA4GMvrMRiWK5Omz
XvauopthhijUDCDJqtg-cWGGuMCOb6dPNkqXW7oEMVCBo-U_e0Nw1dpXh0Goui8pgqvseadEG120d3kVZcke8WcmnIKdi2_VkUxhppOYnXjnAh"
}
You don't have :json
parameter in params
hash: 您在
params
hash中没有:json
参数:
@contactReq = HomeHelper::ContactRequest.new(JSON.parse(params[:json]))
Look at your log above (near Parameters:) 在上方查看您的日志(靠近参数:)
I think you want to pass params
to constructor like this: 我认为您想像这样将
params
传递给构造函数:
@contactReq = HomeHelper::ContactRequest.new(params)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.