我有一个注册控制器(覆盖设计控制器),它应该接收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)
显然,它并没有像我想象的那样创建新用户,但我无法弄清楚原因。如果有人能帮我看看有什么不对,我真的很感激。
答案 0 :(得分:2)
将您的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\"}}"
您未在[{1}}方法中提供registration
require
变化:
user_params
为:
@user = User.new(params[user_params])