【问题标题】:Trouble processing incoming JSON with rails controller使用 rails 控制器处理传入 JSON 时遇到问题
【发布时间】:2014-01-25 23:50:39
【问题描述】:

我有一个注册控制器(覆盖设计控制器),它应该接收 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)

显然它并没有像我想象的那样创建一个新用户,但我不知道为什么。如果有人能帮我看看有什么问题,我将不胜感激。

【问题讨论】:

    标签: ruby-on-rails ruby json devise


    【解决方案1】:

    将您的 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\"}}"
    

    您没有在user_params 方法中提供requireregistration

    编辑

    变化:

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

    到:

    @user = User.new(user_params)
    

    【讨论】:

    • 我也这么认为,但是当您查看网络服务器输出时,您可以看到注册哈希已经在 params 哈希中。我确实按照您的建议尝试了 curl 请求,但结果仍然与我最初的问题相同。
    • 我确实按照您的建议尝试了该建议,但结果与最初相同。
    • 是的,就是这样。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 2014-11-04
    • 2019-04-01
    • 1970-01-01
    • 2016-10-09
    • 1970-01-01
    • 2020-01-30
    • 1970-01-01
    • 2019-04-03
    相关资源
    最近更新 更多