我有一个非常基本的 Rails 应用程序,它使用 Devise 进行用户注册、登录等。我想将 Devise 暴露给 iOS 应用程序。有很多线程是高级解释,但我对 Rails 很陌生。我基本上是向 users/sign_up 发布请求,如下所示:
AFHTTPClient *client = [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@"http://localhost:5000"]];
//[client defaultValueForHeader:@"Accept"];
[client setParameterEncoding:AFJSONParameterEncoding];
NSDictionary *params = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:
@"first.last@gmail.com",
@"testtest",
@"testtest",
nil]
forKeys:[NSArray arrayWithObjects:
@"email",
@"password",
@"password_confirmation",
nil]];
[client registerHTTPOperationClass:[AFHTTPRequestOperation class]];
NSURLRequest *request = [client requestWithMethod:@"GET" path:@"/users/sign_up" parameters:params];
AFHTTPRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
NSLog(@"%@", JSON);
NSLog(@"%@", [response allHeaderFields]);
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
}];
[operation setAcceptableContentTypes:[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/plain",@"text/html", nil]];
[operation start];
当我收到 200 OK 时,这似乎在 iOS 端进行:
2013-01-21 13:38:00.412 Starfish[35773:c07] I
restkit.network:RKHTTPRequestOperation.m:152 GET 'http://localhost:5000/signup?password=testtest&password_confirmation=testtest&email=first.last%40gmail.com'
2013-01-21 13:38:01.073 Starfish[35773:c07] (null)
2013-01-21 13:38:01.073 Starfish[35773:7207] I restkit.network:RKHTTPRequestOperation.m:179 GET 'http://localhost:5000/signup?password=testtest&password_confirmation=testtest&email=first.last%40gmail.com' (200 OK) [0.6611 s]
2013-01-21 13:38:01.074 Starfish[35773:c07] {
"Cache-Control" = "max-age=0, private, must-revalidate";
Connection = close;
"Content-Type" = "text/html; charset=utf-8";
Etag = "\"5dfe88700e41a015a8f524850446e327\"";
Server = "thin 1.5.0 codename Knife";
"Set-Cookie" = "_geo-photo_session=BAh7B0kiD3Nlc3Npb25faWQGOgZFRkkiJTEyZTJjMjFkZjAwZGZlMWIzMTJmNTg5ODNkNjM5OGI0BjsAVEkiEF9jc3JmX3Rva2VuBjsARkkiMTN4eUZGUzVhb1pIdXc1QlJJUnBxSFVrZmYvUDdpeGtaeklxNGhOejFOeEk9BjsARg%3D%3D--5130edc939facbe7004570141c069d40f5f7feca; path=/; HttpOnly";
"X-Request-Id" = a2480798afed4e7aca17f3f1e2d3d44d;
"X-Runtime" = "0.219984";
"X-UA-Compatible" = "IE=Edge";
}
在 Rails 方面:
Started GET "/signup?password=[FILTERED]&password_confirmation=[FILTERED]&email=first.last%40gmail.com" for 127.0.0.1 at 2013-01-21 13:38:00 -0800
13:43:15 web.1 | Processing by Devise::RegistrationsController#new as */*
13:43:15 web.1 | Parameters: {"password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "email"=>"first.last@gmail.com"}
13:43:15 web.1 | Rendered devise/shared/_links.erb (2.1ms)
13:43:15 web.1 | Rendered devise/registrations/new.html.erb within layouts/application (23.7ms)
13:43:15 web.1 | Completed 200 OK in 142ms (Views: 64.9ms | ActiveRecord: 6.9ms)
开箱即用注册:
<h2>Sign up</h2>
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<div><%= f.label :email %><br />
<%= f.email_field :email, :autofocus => true %></div>
<div><%= f.label :password %><br />
<%= f.password_field :password %></div>
<div><%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation %></div>
<div><%= f.submit "Sign up" %></div>
<% end %>
<%= render "devise/shared/links" %>
我不知道 Rails 足以解决的问题是我不希望呈现任何视图。我只是想将注册或登录凭据作为 JSON 对象传递并接收用户已创建或登录的验证。我上面的内容不是创建用户,因为它正在尝试呈现视图。任何帮助将不胜感激。谢谢。