简体   繁体   English

无法从Rails控制器中的POST中提取嵌套的JSON数据

[英]Can't extract nested JSON data from POST in Rails controller

I am trying to configure my controller to process the params sent through a POST from another website. 我正在尝试配置我的控制器来处理从另一个网站通过POST发送的params。 My log shows that the parameters that I receive are as follows: 我的日志显示我收到的参数如下:

{"page_id"=>"8b62f4ac-8588-11e3-a094-12314000b04c", "page_name"=>"test form", "variant"=>"b", "page_url"=>" http://get.xxxxxxx.com/test-form ", "data.json"=>"{\\"name\\":[\\"Dave\\"],\\"email\\":[\\"xxxx@me.com\\"],\\"phone\\":[\\"4447177265\\"],\\"ip_address\\":[\\"64.114.175.126\\"],\\"time_submitted\\":[\\"07:34 AM UTC\\"]}", "data.xml"=>"\\n\\n Dave\\n xxxx@me.com\\n 2507177265\\n 64.114.175.126\\n 07:34 AM UTC\\n"} {“page_id”=>“8b62f4ac-8588-11e3-a094-12314000b04c”,“page_name”=>“test form”,“variant”=>“b”,“page_url”=>“ http://get.xxxxxxx .com / test-form “,”data.json“=>”{\\“name \\”:[\\“Dave \\”],\\“email \\”:[\\“xxxx@me.com \\”],\\ “phone \\”:[\\“4447177265 \\”],\\“ip_address \\”:[\\“64.114.175.126 \\”],\\“time_submitted \\”:[\\“07:34 AM UTC \\”]}“,” data.xml“=>”\\ n \\ n Dave \\ n xxxx@me.com \\ n 2507177265 \\ n 64.114.175.126 \\ n 07:34 AM UTC \\ n“}

Initially I thought that Rails would automatically parse the JSON in the params and I could access them in the normal way. 最初我认为Rails会自动解析params中的JSON,我可以正常方式访问它们。 So I wrote the Registrations Controller like this: 所以我写了这样的注册控制器:

class Api::RegistrationsController < Devise::RegistrationsController

  skip_before_filter :verify_authenticity_token
  respond_to :json

  def create

    @user = User.new(user_params)

    if @user.save
      render json: @user.as_json( email: @user.email), status: 201
      return
    else
      warden.custom_failure!
      render json: @user.errors, status: 422
    end
  end

  def user_params
    params.require(:'data.json').permit(:email, :name, :phone, :comments, :residency, :qualification, :acknowledgement) if params.present?
  end

end

However, it is simply not working at all. 但是,它根本就不起作用。 I get an error undefined method 'permit' for string. 我得到一个错误未定义的方法'permit'为字符串。 So obviously I'm not accessing the JSON correctly. 显然我没有正确访问JSON。 Is it possible that because the JSON is escaped that it's throwing the errors? 是否有可能因为JSON被转义而导致错误?

I've been googling and asking in IRC for a couple of days but I'm not any farther ahead. 我一直在谷歌搜索并在IRC询问了几天,但我不是更进一步。

I can pass a properly formatted JSON to the controller and it works fine (with changes to the require arguments) 我可以将格式正确的JSON传递给控制器​​,它工作正常(需要更改require参数)

I'm stumped since I need to be able to create a new user with the JSON data. 我很难过,因为我需要能够使用JSON数据创建一个新用户。 Any help would be HUGELY appreciated. 任何帮助都会非常感激。 I just don't know what direction to even go from here. 我只是不知道从这里走向何方。

The params.require(:'data.json') returns a JSON body which is a string, however your controller does not interpret the string but expects a Hash. params.require(:'data.json')返回一个JSON主体,它是一个字符串,但是你的控制器不解释字符串但是需要一个Hash。

You can convert the JSON string to a Hash object using the parse class method for JSON like so: 您可以使用JSON的parse类方法将JSON字符串转换为Hash对象,如下所示:

require 'json'
JSON::parse(json_string)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM