2013-01-21 93 views
2

我有一個使用設計的用戶一個非常基本的Rails應用程序註冊,登錄等。我想揭露設計到iOS應用程序。有很多線程是高層次的解釋,但我對Rails很陌生。我基本上張貼到用戶的請求/ sign_up像這樣:使用設計註冊,登錄和註銷與iOS應用

AFHTTPClient *client = [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@"http://localhost:5000"]]; 
//[client defaultValueForHeader:@"Accept"]; 
[client setParameterEncoding:AFJSONParameterEncoding]; 
NSDictionary *params = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects: 
                  @"[email protected]", 
                  @"testtest", 
                  @"testtest", 
                  nil] 
                forKeys:[NSArray arrayWithObjects: 
                  @"email", 
                  @"password", 
                  @"password_confirmation", 
                  nil]]; 

[client registerHTTPOperationClass:[AFHTTPRequestOperation class]]; 
NSURLRequest *request = [client requestWithMethod:@"GET" path:@"https://stackoverflow.com/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]; 

這似乎是想通過在iOS端我收到一個200 OK:

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

而且在鐵軌上側:

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"=>"[email protected]"} 
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對象,並接收用戶創建或登錄驗證。上面我所,因爲它試圖渲染視圖不創建用戶。任何幫助將不勝感激。謝謝。

+0

您可以發佈從Rails的您的註冊方法是什麼?有2個部分,iOS應用程序請求JSON,Rails響應。 – MishieMoo

+0

這就是我卡住的地方。這是Devise安裝的開箱即用型號/視圖/控制器。 –

+0

我在說你的實際控制器代碼(而不是視圖)。不是每個人都會記住Devise爲您提供的開箱即用功能。有機會你只需要編輯一個'respond_to'。 – MishieMoo

回答

4

我在做類似的事情,並有註冊它使用JSON(這應該轉化到iOS容易)迄今通過工作捲曲部分,也可通過瀏覽器。從NeverBe使用的this answer變形例:

我改性配置/初始化/ devise.rb和添加的行:

config.navigational_formats = ["*/*", "/", :html, :json] 

我改性配置/ routes.rb中和替換這line:

devise_for :users 

with this line:

devise_for :users, :controllers => {:registrations => "registrations"} 

我創建了一個新的文件應用程序/控制器/ registrations_controller.rb

class RegistrationsController < Devise::RegistrationsController 

    def create 
    respond_to do |format| 
     format.html { 
     super 
     } 
     format.json { 
     if params[:user][:email].nil? 
      render :status => 400, 
       :json => {:message => 'User request must contain the user email.'} 
      return 
     elsif params[:user][:password].nil? 
      render :status => 400, 
       :json => {:message => 'User request must contain the user password.'} 
      return 
     end 

     if params[:user][:email] 
      duplicate_user = User.find_by_email(params[:user][:email]) 
      unless duplicate_user.nil? 
      render :status => 409, 
        :json => {:message => 'Duplicate email. A user already exists with that email address.'} 
      return 
      end 
     end 

     @user = User.create(params[:user]) 

     if @user.save 
      render :json => {:user => @user} 
     else 
      render :status => 400, 
       :json => {:message => @user.errors.full_messages} 
     end 
     } 
    end 
    end 
end 

附加說明:您仍然得到錯誤,而不所有自定義錯誤處理代碼甚至處理,但我想返回狀態代碼中的錯誤(400和409),而不是200自定義錯誤消息這是我從默認色器件行爲得到。