2012-03-21 66 views
0

我在iOS和Rails中使用Restkit來搞亂。我不能爲我的生活弄清楚爲什麼我的代碼不工作。基本上我有在IOS一個ProfileModel類和我設置爲它的對象映射和調用的方法如下:RestKit putObject不能與Rails模型一起工作

-(IBAction)saveButton:(id)sender 
{ 
NSLog(@"Editprofile save button clicked"); 

RKObjectMapping *objectmappingSerialProfile = [RKObjectMapping mappingForClass:[ProfileModel class]]; 

[objectmappingSerialProfile mapKeyPath:@"location" toAttribute:@"country"]; 
[objectmappingSerialProfile mapKeyPath:@"descriptionText" toAttribute:@"description"]; 
[objectmappingSerialProfile mapKeyPath:@"facebookID" toAttribute:@"facebook"]; 
[objectmappingSerialProfile mapKeyPath:@"username" toAttribute:@"name"]; 
[objectmappingSerialProfile mapKeyPath:@"twitterID" toAttribute:@"twittername"]; 
[objectmappingSerialProfile mapKeyPath:@"website" toAttribute:@"website"]; 

NSDictionary *authTokenParam = [NSDictionary dictionaryWithKeysAndObjects:@"auth_token",@"pb9mp459iUj6NnmPk5C2", nil]; 

[[RKObjectManager sharedManager].router routeClass:[ProfileModel class] toResourcePath:[@"/modifyProfile.json" appendQueryParams:authTokenParam] forMethod:RKRequestMethodPUT]; 

[[RKObjectManager sharedManager].mappingProvider setSerializationMapping:objectmappingSerialProfile forClass:[ProfileModel class]]; 

[[RKObjectManager sharedManager] setSerializationMIMEType:RKMIMETypeJSON]; 

[[RKObjectManager sharedManager] putObject:self.navProfile delegate:nil]; 

} 

當運行此代碼我在Xcode

得到在調試器窗口下面的錯誤消息
2012-03-20 23:51:22.815 Someprogram[3684:fe03] Editprofile save button clicked 
2012-03-20 23:51:22.921 Someprogram[3684:fe03] W  restkit.network:RKObjectLoader.m:241 Unable to find parser for MIME Type 'text/html' 
2012-03-20 23:51:22.921 Someprogram[3684:fe03] W  restkit.network:RKObjectLoader.m:262 Encountered unexpected response with status code: 500 (MIME Type: text/html) 

我用鋼軌0.9.3和我的軌道窗口答覆如下

Started PUT "/modifyProfile.json?auth_token=pb9mp459iUj6NnmPk5C2" for 127.0.0.1 at 2012-03-20 23:51:22 +0000 
Processing by ProfilesController#update as JSON 
    Parameters: {"country"=>"earth2", "name"=>"AdiUpdated2", "twittername"=>"abc", "website"=>"hmmm", "description"=>"abcc", "facebook"=>"abcd", "auth_token"=>"pb9mp459iUj6NnmPk5C2", "profile"=>{"name"=>"AdiUpdated2", "description"=>"abcc", "twittername"=>"abc", "facebook"=>"abcd", "website"=>"hmmm", "country"=>"earth2"}} 
WARNING: Can't verify CSRF token authenticity 
    User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = 9 LIMIT 1 
    (0.1ms) begin transaction 
    SOLR Request (2.2ms) [ path=#<RSolr::Client:0x007fc565d65f88> parameters={data: <?xml version="1.0" encoding="UTF-8"?><add><doc><field name="id">User 9</field><field name="type">User</field><field name="type">ActiveRecord::Base</field><field name="class_name">User</field></doc></add>, headers: {"Content-Type"=>"text/xml"}, method: post, params: {:wt=>:ruby}, query: wt=ruby, path: update, uri: http://localhost:8982/solr/update?wt=ruby, open_timeout: , read_timeout: } ] 
    (0.1ms) rollback transaction 
Completed 500 Internal Server Error in 22ms 

Errno::ECONNREFUSED (Connection refused - connect(2)): 
    /Users/Adi/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/net/http.rb:762:in `initialize' 
    /Users/Adi/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/net/http.rb:762:in `open' 
    /Users/Adi/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/net/http.rb:762:in `block in connect' 
    /Users/Adi/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/timeout.rb:54:in `timeout' 
    /Users/Adi/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/timeout.rb:99:in `timeout' 
    /Users/Adi/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/net/http.rb:762:in `connect' 
    /Users/Adi/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/net/http.rb:755:in `do_start' 
    /Users/Adi/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/net/http.rb:744:in `start' 
    /Users/Adi/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/net/http.rb:1284:in `request' 
    rsolr (1.0.7) lib/rsolr/connection.rb:15:in `execute' 
    sunspot_rails (1.3.0) lib/sunspot/rails/solr_instrumentation.rb:14:in `block in execute_with_as_instrumentation' 
    activesupport (3.2.1) lib/active_support/notifications.rb:123:in `block in instrument' 
    activesupport (3.2.1) lib/active_support/notifications/instrumenter.rb:20:in `instrument' 
    activesupport (3.2.1) lib/active_support/notifications.rb:123:in `instrument' 
    sunspot_rails (1.3.0) lib/sunspot/rails/solr_instrumentation.rb:12:in `execute_with_as_instrumentation' 
    rsolr (1.0.7) lib/rsolr/client.rb:166:in `execute' 
    rsolr (1.0.7) lib/rsolr/client.rb:161:in `send_and_receive' 
    (eval):2:in `post' 
    rsolr (1.0.7) lib/rsolr/client.rb:67:in `update' 
    rsolr (1.0.7) lib/rsolr/client.rb:87:in `add' 
    sunspot (1.3.0) lib/sunspot/indexer.rb:101:in `add_documents' 
    sunspot (1.3.0) lib/sunspot/indexer.rb:26:in `add' 
    sunspot (1.3.0) lib/sunspot/session.rb:91:in `index' 
    sunspot (1.3.0) lib/sunspot/session_proxy/abstract_session_proxy.rb:11:in `index' 
    sunspot (1.3.0) lib/sunspot.rb:184:in `index' 
    sunspot_rails (1.3.0) lib/sunspot/rails/searchable.rb:372:in `solr_index' 
    sunspot_rails (1.3.0) lib/sunspot/rails/searchable.rb:470:in `perform_index_tasks' 
    activesupport (3.2.1) lib/active_support/callbacks.rb:427:in `_run__3891665525441134440__save__2817329553089450813__callbacks' 
    activesupport (3.2.1) lib/active_support/callbacks.rb:405:in `__run_callback' 
    activesupport (3.2.1) lib/active_support/callbacks.rb:385:in `_run_save_callbacks' 
    activesupport (3.2.1) lib/active_support/callbacks.rb:81:in `run_callbacks' 
    activerecord (3.2.1) lib/active_record/callbacks.rb:264:in `create_or_update' 
    activerecord (3.2.1) lib/active_record/persistence.rb:84:in `save' 
    activerecord (3.2.1) lib/active_record/validations.rb:50:in `save' 
    activerecord (3.2.1) lib/active_record/attribute_methods/dirty.rb:22:in `save' 
    activerecord (3.2.1) lib/active_record/transactions.rb:241:in `block (2 levels) in save' 
    activerecord (3.2.1) lib/active_record/transactions.rb:295:in `block in with_transaction_returning_status' 
    activerecord (3.2.1) lib/active_record/connection_adapters/abstract/database_statements.rb:190:in `transaction' 
    activerecord (3.2.1) lib/active_record/transactions.rb:208:in `transaction' 
    activerecord (3.2.1) lib/active_record/transactions.rb:293:in `with_transaction_returning_status' 
    activerecord (3.2.1) lib/active_record/transactions.rb:241:in `block in save' 
    activerecord (3.2.1) lib/active_record/transactions.rb:252:in `rollback_active_record_state!' 
    activerecord (3.2.1) lib/active_record/transactions.rb:240:in `save' 
    devise (2.0.4) lib/devise/models/rememberable.rb:58:in `forget_me!' 
    devise (2.0.4) lib/devise/controllers/rememberable.rb:32:in `forget_me' 
    devise (2.0.4) lib/devise/hooks/forgetable.rb:7:in `block in <top (required)>' 
    warden (1.1.1) lib/warden/hooks.rb:14:in `call' 
    warden (1.1.1) lib/warden/hooks.rb:14:in `block in _run_callbacks' 
    warden (1.1.1) lib/warden/hooks.rb:9:in `each' 
    warden (1.1.1) lib/warden/hooks.rb:9:in `_run_callbacks' 
    warden (1.1.1) lib/warden/manager.rb:53:in `_run_callbacks' 
    warden (1.1.1) lib/warden/proxy.rb:243:in `block in logout' 
    warden (1.1.1) lib/warden/proxy.rb:241:in `each' 
    warden (1.1.1) lib/warden/proxy.rb:241:in `logout' 
    devise (2.0.4) lib/devise/controllers/helpers.rb:156:in `sign_out_all_scopes' 
    devise (2.0.4) lib/devise/controllers/helpers.rb:261:in `handle_unverified_request' 
    actionpack (3.2.1) lib/action_controller/metal/request_forgery_protection.rb:78:in `verify_authenticity_token' 
    activesupport (3.2.1) lib/active_support/callbacks.rb:407:in `_run__1101336661489218557__process_action__1327857175712471554__callbacks' 
    activesupport (3.2.1) lib/active_support/callbacks.rb:405:in `__run_callback' 
    activesupport (3.2.1) lib/active_support/callbacks.rb:385:in `_run_process_action_callbacks' 
    activesupport (3.2.1) lib/active_support/callbacks.rb:81:in `run_callbacks' 
    actionpack (3.2.1) lib/abstract_controller/callbacks.rb:17:in `process_action' 
    actionpack (3.2.1) lib/action_controller/metal/rescue.rb:29:in `process_action' 
    actionpack (3.2.1) lib/action_controller/metal/instrumentation.rb:30:in `block in process_action' 
    activesupport (3.2.1) lib/active_support/notifications.rb:123:in `block in instrument' 
    activesupport (3.2.1) lib/active_support/notifications/instrumenter.rb:20:in `instrument' 
    activesupport (3.2.1) lib/active_support/notifications.rb:123:in `instrument' 
    actionpack (3.2.1) lib/action_controller/metal/instrumentation.rb:29:in `process_action' 
    actionpack (3.2.1) lib/action_controller/metal/params_wrapper.rb:205:in `process_action' 
    activerecord (3.2.1) lib/active_record/railties/controller_runtime.rb:18:in `process_action' 
    actionpack (3.2.1) lib/abstract_controller/base.rb:121:in `process' 
    actionpack (3.2.1) lib/abstract_controller/rendering.rb:45:in `process' 
    actionpack (3.2.1) lib/action_controller/metal.rb:203:in `dispatch' 
    actionpack (3.2.1) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch' 
    actionpack (3.2.1) lib/action_controller/metal.rb:246:in `block in action' 
    actionpack (3.2.1) lib/action_dispatch/routing/route_set.rb:66:in `call' 
    actionpack (3.2.1) lib/action_dispatch/routing/route_set.rb:66:in `dispatch' 
    actionpack (3.2.1) lib/action_dispatch/routing/route_set.rb:30:in `call' 
    journey (1.0.1) lib/journey/router.rb:68:in `block in call' 
    journey (1.0.1) lib/journey/router.rb:56:in `each' 
    journey (1.0.1) lib/journey/router.rb:56:in `call' 
    actionpack (3.2.1) lib/action_dispatch/routing/route_set.rb:589:in `call' 
    warden (1.1.1) lib/warden/manager.rb:35:in `block in call' 
    warden (1.1.1) lib/warden/manager.rb:34:in `catch' 
    warden (1.1.1) lib/warden/manager.rb:34:in `call' 
    actionpack (3.2.1) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call' 
    rack (1.4.1) lib/rack/etag.rb:23:in `call' 
    rack (1.4.1) lib/rack/conditionalget.rb:35:in `call' 
    actionpack (3.2.1) lib/action_dispatch/middleware/head.rb:14:in `call' 
    actionpack (3.2.1) lib/action_dispatch/middleware/params_parser.rb:21:in `call' 
    actionpack (3.2.1) lib/action_dispatch/middleware/flash.rb:242:in `call' 
    rack (1.4.1) lib/rack/session/abstract/id.rb:205:in `context' 
    rack (1.4.1) lib/rack/session/abstract/id.rb:200:in `call' 
    actionpack (3.2.1) lib/action_dispatch/middleware/cookies.rb:338:in `call' 
    activerecord (3.2.1) lib/active_record/query_cache.rb:64:in `call' 
    activerecord (3.2.1) lib/active_record/connection_adapters/abstract/connection_pool.rb:443:in `call' 
    actionpack (3.2.1) lib/action_dispatch/middleware/callbacks.rb:28:in `block in call' 
    activesupport (3.2.1) lib/active_support/callbacks.rb:405:in `_run__632645031902289657__call__2817329553089450813__callbacks' 
    activesupport (3.2.1) lib/active_support/callbacks.rb:405:in `__run_callback' 
    activesupport (3.2.1) lib/active_support/callbacks.rb:385:in `_run_call_callbacks' 
    activesupport (3.2.1) lib/active_support/callbacks.rb:81:in `run_callbacks' 
    actionpack (3.2.1) lib/action_dispatch/middleware/callbacks.rb:27:in `call' 
    actionpack (3.2.1) lib/action_dispatch/middleware/reloader.rb:65:in `call' 
    actionpack (3.2.1) lib/action_dispatch/middleware/remote_ip.rb:31:in `call' 
    actionpack (3.2.1) lib/action_dispatch/middleware/debug_exceptions.rb:16:in `call' 
    actionpack (3.2.1) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call' 
    railties (3.2.1) lib/rails/rack/logger.rb:26:in `call_app' 
    railties (3.2.1) lib/rails/rack/logger.rb:16:in `call' 
    actionpack (3.2.1) lib/action_dispatch/middleware/request_id.rb:22:in `call' 
    rack (1.4.1) lib/rack/methodoverride.rb:21:in `call' 
    rack (1.4.1) lib/rack/runtime.rb:17:in `call' 
    activesupport (3.2.1) lib/active_support/cache/strategy/local_cache.rb:72:in `call' 
    rack (1.4.1) lib/rack/lock.rb:15:in `call' 
    actionpack (3.2.1) lib/action_dispatch/middleware/static.rb:53:in `call' 
    railties (3.2.1) lib/rails/engine.rb:479:in `call' 
    railties (3.2.1) lib/rails/application.rb:220:in `call' 
    rack (1.4.1) lib/rack/content_length.rb:14:in `call' 
    railties (3.2.1) lib/rails/rack/log_tailer.rb:14:in `call' 
    rack (1.4.1) lib/rack/handler/webrick.rb:59:in `service' 
    /Users/Adi/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service' 
    /Users/Adi/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run' 
    /Users/Adi/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread' 


    Rendered /Users/Adi/.rvm/gems/ruby-1.9.3-p0/gems/actionpack-3.2.1/lib/action_dispatch/middleware/templates/rescues/_trace.erb (3.9ms) 
    Rendered /Users/Adi/.rvm/gems/ruby-1.9.3-p0/gems/actionpack-3.2.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (1.0ms) 
    Rendered /Users/Adi/.rvm/gems/ruby-1.9.3-p0/gems/actionpack-3.2.1/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (6.9ms) 

不過,如果我使用了一個名爲RESTClient實現在Mac上執行相同的PUT請求程序。它工作正常,並在窗口Rails的迴應是:

Started PUT "/modifyProfile.json?auth_token=pb9mp459iUj6NnmPk5C2" for 127.0.0.1 at 2012-03-21 00:02:57 +0000 
Processing by ProfilesController#update as JSON 
    Parameters: {"country"=>"earth22", "description"=>"abcc", "twittername"=>"abc", "facebook"=>"abcd", "website"=>"hmmm", "name"=>"AdiUpdated2", "auth_token"=>"pb9mp459iUj6NnmPk5C2", "profile"=>{"name"=>"AdiUpdated2", "description"=>"abcc", "twittername"=>"abc", "facebook"=>"abcd", "website"=>"hmmm", "country"=>"earth22"}} 
WARNING: Can't verify CSRF token authenticity 
    User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."authentication_token" = 'pb9mp459iUj6NnmPk5C2' LIMIT 1 
    Profile Load (0.1ms) SELECT "profiles".* FROM "profiles" WHERE "profiles"."user_id" = 9 LIMIT 1 
    (0.0ms) begin transaction 
    (0.0ms) commit transaction 
    Rendered profiles/update.json.rabl (1.9ms) 
Completed 200 OK in 9ms (Views: 4.4ms | ActiveRecord: 0.4ms | Solr: 0.0ms) 

我看到的唯一區別是是與它建立了某種問題的太陽黑子Solr的。但請求代碼似乎是相同的,所以爲什麼Rails對此做出不同的響應?任何人都可以幫忙嗎?

對長長的代碼行道歉,但不確定你們可能想看看我做錯了什麼?

非常感謝您的期待

回答

1

Ooookkk。這很尷尬。但是,如果有人面臨同一問題,則發佈此信息。

由於忘記打開Solr服務器,因此出現此問題。糟糕!一旦服務器啓動並運行,它就可以工作。

但我仍然想知道爲什麼這是通過Restkit而不是通過RESTClient程序運行時的問題。

相關問題