从JSON创建Devise用户 [英] Create User in Devise from JSON

查看:117
本文介绍了从JSON创建Devise用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将我的Rails 3.1应用程序与我的iOS应用程序集成在一起,运行Devise进行用户身份验证。我希望用户能够从应用程序注册,然后我可以存储这些凭据以便稍后登录。

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.

使用RestKit,我这样做: p>

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];
}

/users.json url转到: user_registration POST /users(.:format){:action =>create,:controller =>devise / registrations} 根据耙子路线)。看来,post调用接受不同的格式,所以我认为它会接受JSON。我的帖子请求被序列化为JSON,并被发送。服务器得到它,这是日志:

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"=>"test1@test.com"}
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应用程序得到的响应是:响应:{电子邮件:[不能为空],密码:[不能为空]} 。但是密码和电子邮件不是空白的,服务器成功获取。所以,一些事情是不正常的,我不知道去哪里。如何使用JSON创建用户?

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?

感谢您的帮助!

推荐答案

您的rails应用程序期待JSON格式:

Your rails app is expecting JSON in this form :

{"user":{"email":"test@example.com", "password":"Test123", "password_confirmation":"Test123"}}

但是默认情况下RestKit发送:

But by default RestKit is sending this :

{"email":"test@example.com", "password":"Test123", "password_confirmation":"Test123"}

要添加user,您只需要设置rootKeyPath如下所示:

To add "user" you just need to set the rootKeyPath like this:

RKObjectMapping *userSerializationMapping = [userMapping inverseMapping];
userSerializationMapping.rootKeyPath = @"user";
[[RKObjectManager sharedManager].mappingProvider setSerializationMapping:userSerializationMapping forClass:[User class]];

这篇关于从JSON创建Devise用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆