从 JSON 在 Devise 中创建用户
我正在努力将运行 Devise 的 Rails 3.1 应用程序与我的 iOS 应用程序集成以进行用户身份验证。我希望用户能够从应用程序注册,然后我可以存储这些凭据以便稍后登录。
使用 RestKit,我这样做:
-(IBAction)registerUser:(id)sender {
NSDictionary *params = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:
self.email.text,
self.password.text,
self.confirmPassword.text,
nil]
forKeys:[NSArray arrayWithObjects:
@"email",
@"password",
@"password_confirmation",
nil]];
[[RKClient sharedClient] post:@"/users.json" params:params delegate:self];
}
/users.json
url 转到: user_registration POST /users(.:format) {:action=>"create", :controller=>; “devise/registrations”}
(根据 rake 路线)。看来 post 调用接受不同的格式,所以我假设它会接受 JSON。我的 Post 请求被序列化为 JSON,然后发送出去。服务器获取它,这是日志:
Started POST "/users.json" for 129.21.84.10 at 2012-01-12 15:33:57 -0500
Processing by Devise::RegistrationsController#create as JSON
Parameters: {"password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "email"=>"[email protected]"}
WARNING: Can't verify CSRF token authenticity
User Load (0.9ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
(0.3ms) BEGIN
(0.2ms) COMMIT
(0.2ms) BEGIN
(0.4ms) ROLLBACK
Completed 422 Unprocessable Entity in 93ms (Views: 3.8ms | ActiveRecord: 10.2ms)
我收到 422 错误,并且未创建我的用户。我的 iOS 应用程序得到的响应是:响应:{“email”:[“不能为空”],“密码”:[“不能为空”]}
。但密码和邮箱不为空,服务器已成功获取。所以,有些事情不太正常,我不知道该去哪里。如何使用 JSON 创建用户?
感谢您的帮助!
I'm working on integrating my Rails 3.1 app running Devise for user authentication with my iOS app. I'd like the user to be able to register from the application, and then I can store those credentials to login later.
Using RestKit, I do this:
-(IBAction)registerUser:(id)sender {
NSDictionary *params = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:
self.email.text,
self.password.text,
self.confirmPassword.text,
nil]
forKeys:[NSArray arrayWithObjects:
@"email",
@"password",
@"password_confirmation",
nil]];
[[RKClient sharedClient] post:@"/users.json" params:params delegate:self];
}
The /users.json
url goes to: user_registration POST /users(.:format) {:action=>"create", :controller=>"devise/registrations"}
(according to rake routes). It appears that the post call accepts different formats, so I assume it will accept JSON. My Post request is serialized as JSON, and sent off. The server gets it, and this is the log:
Started POST "/users.json" for 129.21.84.10 at 2012-01-12 15:33:57 -0500
Processing by Devise::RegistrationsController#create as JSON
Parameters: {"password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "email"=>"[email protected]"}
WARNING: Can't verify CSRF token authenticity
User Load (0.9ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
(0.3ms) BEGIN
(0.2ms) COMMIT
(0.2ms) BEGIN
(0.4ms) ROLLBACK
Completed 422 Unprocessable Entity in 93ms (Views: 3.8ms | ActiveRecord: 10.2ms)
I get a 422 error, and my user is not created. The response my iOS app gets is: Response: {"email":["can't be blank"],"password":["can't be blank"]}
. But the password and email isn't blank, the server got them successfully. So, something isn't working right, and I'm not sure where to go. How can I create the user using JSON?
Thanks for helping!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我自己也在做同样的事情——通过 iPhone 应用程序在 Devise 中创建用户。我通过 POSTing json 让它工作起来,如下所示:
和 URL:
http://localhost:3000/users.json< /a>
,我的帖子的请求标头 Content-Type 是“application/json”。
所有标准设计配置。
I'm just working on the same thing myself - creating users in Devise from an iPhone app. I've got it working a treat by POSTing json that looks like this:
and the URL:
http://localhost:3000/users.json
and the request header Content-Type of my post is 'application/json'.
All standard Devise config.
您的 Rails 应用程序需要这种形式的 JSON:
但默认情况下 RestKit 会发送以下内容:
要添加“用户”,您只需设置 rootKeyPath,如下所示:
Your rails app is expecting JSON in this form :
But by default RestKit is sending this :
To add "user" you just need to set the rootKeyPath like this: