Hi,
i'm trying to build an application that interact with a Ruby on Rails backend.
Actually i have a problem with authentication.
I've create a controller that populate a table from an xml downloaded file. If you click on edit button you can delete rows and there's a call to the sever to delete the record on the database. Everything works, but if i reactivate the authentication filter on the server i cannot authenticate with the code written below.
Actually the server do a redirect to compile a form with login and password.
The authentication requested is HTTP Basic and i've confurated the server to accept every combination on username and password.
Code:
authenticate_or_request_with_http_basic do |username, password|
true
end
This is my controller code:
Code:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
int storyIndex = [indexPath indexAtPosition: [indexPath length] - 1];
NSString *objectId = [[stories objectAtIndex: storyIndex] objectForKey: @"id"];
NSUInteger index = [objectId intValue];
NSString *preUrl = [NSString stringWithFormat:@"http://localhost:3000/admin/admin_page/destroy/%d", index];
NSLog(@"Destroy story at index: %@", preUrl);
NSURL *url = [NSURL URLWithString: preUrl];
NSURLCredential *userCredentials = [NSURLCredential credentialWithUser:@"macteo"
password:@"password"
persistence:NSURLCredentialPersistenceForSession];
NSURLProtectionSpace *space = [[NSURLProtectionSpace alloc] initWithHost:@"localhost"
port:3000
protocol:@"http"
realm:nil
authenticationMethod: NSURLAuthenticationMethodDefault];
[[NSURLCredentialStorage sharedCredentialStorage] setCredential:userCredentials
forProtectionSpace:space];
NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/xml" forHTTPHeaderField:@"Content-Type"];
/* [request setHTTPBody:[[self xml] dataUsingEncoding:NSUTF8StringEncoding]]; */
NSURLResponse * response;
NSError * error;
[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
// We're ignoring all the outputs because we don't really care.
// Delete the row from the data source
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
}
if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
This is the Ruby on Rails serve log
Code:
Processing AdminPageController#destroy (for 127.0.0.1 at 2008-08-24 12:50:47) [post]
Session ID: BAh7BiIKZmxhc2hJQzonQWN0aW9uQ29udHJvbGxlcjo6Rmxhc2g6OkZsYXNo
SGFzaHsABjoKQHVzZWR7AA==--94d9d598af90877c999f52867ccabb58fd6b0c74
Parameters: {"action"=>"destroy", "id"=>"8", "controller"=>"admin_page"}
Filter chain halted as [:http_basic_authentication] rendered_or_redirected.
Completed in 0.00092 (1090 reqs/sec) | Rendering: 0.00063 (68%) | DB: 0.00000 (0%) | 401 Unauthorized [http://localhost/admin/admin_page/destroy/8]
Thanks,
MacTeo