当有效负载没有空值时,JSON解析会抛出nil错误

时间:2015-07-21 13:16:46

标签: ruby-on-rails json ruby-on-rails-4

我看到这个相当奇怪的问题构建我的第一个rails 4演示应用程序,我有通过angular进行POST请求,但是在控制器上解析它会引发错误

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'

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

发布此数据的角度代码段是

        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."
            }
        })

我收到错误的控制器

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

我在日志中没有任何nil或空白字段,也没有在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"
}

1 个答案:

答案 0 :(得分:1)

:json哈希中没有params参数:

@contactReq = HomeHelper::ContactRequest.new(JSON.parse(params[:json]))

查看上面的日志(靠近参数:)

我想你想把params传递给像这样的构造函数:

@contactReq = HomeHelper::ContactRequest.new(params)