繁体   English   中英

使用Rails控制器处理传入的JSON时出现问题

[英]Trouble processing incoming JSON with rails controller

我有一个Registrations控制器(重写devise控制器),该控制器应接收JSON哈希,然后注册一个新用户。 但是,新用户保存总是失败,但是我不知道为什么。

这是我的控制器:

class Api::V1::RegistrationsController < Devise::RegistrationsController

  skip_before_filter :verify_authenticity_token
  respond_to :json

  def create

    @user = User.new(params[user_params])

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

  def user_params
    params.require[:registration].permit(:email, :password, :name, :phone, :acknowledgement) if params.present?
  end

end

我的用户模型包括对电子邮件,电话,姓名和确认的验证

为了测试控制器,我使用以下命令:

curl -v -H 'Content-Type: application/json' -H 'Accept: application/json' -X POST http://localhost:3000/api/v1/registrations -d "{\"email\":\"user1@example.com\",\"name\":\"anotheruser\",\"phone\":\"250.717.7265\",\"acknowledgement\":\"true\"}"

我从运行命令得到以下输出:

* Adding handle: conn: 0x7fef04004000
* Adding handle: send: 0
* Adding handle: recv: 0
* Curl_addHandleToPipeline: length: 1
* - Conn 0 (0x7fef04004000) send_pipe: 1, recv_pipe: 0
* About to connect() to localhost port 3000 (#0)
*   Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 3000 (#0)
> POST /api/v1/registrations HTTP/1.1
> User-Agent: curl/7.30.0
> Host: localhost:3000
> Content-Type: application/json
> Accept: application/json
> Content-Length: 98
>
* upload completely sent off: 98 out of 98 bytes
< HTTP/1.1 422 Unprocessable Entity
< X-Frame-Options: SAMEORIGIN
< X-Xss-Protection: 1; mode=block
< X-Content-Type-Options: nosniff
< X-Ua-Compatible: chrome=1
< Content-Type: application/json; charset=utf-8
< Cache-Control: no-cache
< X-Request-Id: 409196ef-657d-4420-be59-9ff632699d23
< X-Runtime: 0.013133
* Server WEBrick/1.3.1 (Ruby/2.0.0/2013-11-22) is not blacklisted
< Server: WEBrick/1.3.1 (Ruby/2.0.0/2013-11-22)
< Date: Sat, 25 Jan 2014 23:47:34 GMT
< Content-Length: 168
< Connection: Keep-Alive
< Set-Cookie: request_method=POST; path=/
<
* Connection #0 to host localhost left intact
{"email":["can't be blank","can't be blank"],"name":["can't be blank"],"phone":["can't be blank","Must be in 555.555.5555 format"],"acknowledgement":["can't be blank"]}

我的网络服务器给我以下输出:

Started POST "/api/v1/registrations" for 127.0.0.1 at 2014-01-25 15:43:43 -0800
Processing by Api::V1::RegistrationsController#create as JSON
  Parameters: {"email"=>"user1@example.com", "name"=>"anotheruser", "phone"=>"250.717.7265", "acknowledgement"=>"true", "registration"=>{"email"=>"user1@example.com", "name"=>"anotheruser", "phone"=>"250.717.7265", "acknowledgement"=>"true"}}
   (0.1ms)  begin transaction
   (0.1ms)  rollback transaction
Completed 422 Unprocessable Entity in 7ms (Views: 0.2ms | ActiveRecord: 0.1ms)

显然,它并没有像我想的那样创建新用户,但我不知道为什么。 如果有人可以帮助我找出问题所在,我将不胜感激。

更改您的curl请求,使其看起来像这样:

curl -v -H 'Content-Type: application/json' -H 'Accept: application/json'\
 -X POST http://localhost:3000/api/v1/registrations 
\-d "{\"registration\":{\"email\":\"user1@example.com\",\"name\":\"anotheruser\",\"phone\":\"250.717.7265\",\"acknowledgement\":\"true\"}}"

你是不是提供了registration ,你require在你的user_params方法

编辑

更改:

@user = User.new(params[user_params])

至:

@user = User.new(user_params)

暂无
暂无

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

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