我在做广告。我设置了一切,因为我认为它是正确的,然而,当我点击玻色子"cadastrar发音“,它返回这个错误"got 422”。已经对错误进行了研究,但根本无法修复。这里有人经历过这一切吗?
这是我的web服务
#import "JVWebService.h"
#import <RestKit/RestKit.h>
#import "AppDelegate.h"
#import "JVUtils.h"
#import "Ads.h"
static NSString *kServerURL = @"http://localhost:3000";
@interface JVWebService ()
@property (strong, nonatomic) RKObjectManager *restKitObjectManager;
@property (strong, nonatomic) NSDictionary *adAttributes;
@property (strong, nonatomic) NSDictionary *postAdAttributes;
@property (strong, nonatomic) NSDictionary *userAttributes;
@property (strong, nonatomic) NSDictionary *postUserAttributes;
@end
#define kSuccessStatusCode RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)
@implementation JVWebService
+ (instancetype)sharedService {
static JVWebService *sharedService = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedService = [[self alloc] init];
[AFNetworkActivityIndicatorManager sharedManager].enabled = YES;
sharedService.restKitObjectManager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:kServerURL]];
[sharedService.restKitObjectManager.HTTPClient setAuthorizationHeaderWithUsername:[[[AppDelegate sharedDelegate] currentUser] email]
password:[[[AppDelegate sharedDelegate] currentUser] password]];
});
return sharedService;
}
#pragma mark - User
- (void)getUserForEmail:(NSString *)email andPassword:(NSString *)password {
RKObjectMapping *objectMapping = [RKObjectMapping mappingForClass:User.class];
[objectMapping addAttributeMappingsFromDictionary:self.userAttributes];
RKObjectMapping *requestMapping = [RKObjectMapping requestMapping];
[requestMapping addAttributeMappingsFromDictionary:self.postUserAttributes];
NSString *path = @"/users/sign_in.json";
RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:requestMapping
objectClass:User.class
rootKeyPath:@"user"
method:RKRequestMethodAny];
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:objectMapping
method:RKRequestMethodAny
pathPattern:path
keyPath:@"user"
statusCodes:kSuccessStatusCode];
[self.restKitObjectManager addRequestDescriptor:requestDescriptor];
[self.restKitObjectManager addResponseDescriptor:responseDescriptor];
User *user = [User new];
user.email = email;
user.password = password;
[[NSUserDefaults standardUserDefaults] setObject:[[NSUUID UUID] UUIDString] forKey:@"authencity_token"];
NSDictionary *params = @{@"authenticity_token" : [[NSUserDefaults standardUserDefaults] objectForKey:@"authencity_token"]};
[self.restKitObjectManager.HTTPClient setAuthorizationHeaderWithUsername:email password:password];
[self.restKitObjectManager postObject:user path:path parameters:params success:^(RKObjectRequestOperation *operation,
RKMappingResult *result){
User *user = (User *)result.array.firstObject;
user.password = password;
[[AppDelegate sharedDelegate] login:user];
[[AppDelegate sharedDelegate] setLoggedViaFacebook:NO];
if ([self.serviceDelegate respondsToSelector:@selector(successfulRequestDidReturnObject:)])
[self.serviceDelegate successfulRequestDidReturnObject:user];
} failure:^(RKObjectRequestOperation *operation, NSError *error){
RKLogError(@"Operation failed with error: %@", error);
if ([self.serviceDelegate respondsToSelector:@selector(requestDidFailWithError:)])
[self.serviceDelegate requestDidFailWithError:error];
}];
[self.restKitObjectManager removeResponseDescriptor:responseDescriptor];
}
- (void)postAd:(Ads *)ad {
NSString *path = @"/ads.json";
RKObjectMapping *objectMapping = [RKObjectMapping mappingForClass:Ads.class];
[objectMapping addAttributeMappingsFromDictionary:self.adAttributes];
RKObjectMapping *requestMapping = [RKObjectMapping requestMapping];
[requestMapping addAttributeMappingsFromDictionary:self.postAdAttributes];
RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:requestMapping
objectClass:Ads.class
rootKeyPath:@"ad"
method:RKRequestMethodAny];
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:objectMapping
method:RKRequestMethodAny
pathPattern:path
keyPath:@"ad"
statusCodes:kSuccessStatusCode];
[self.restKitObjectManager addRequestDescriptor:requestDescriptor];
[self.restKitObjectManager addResponseDescriptor:responseDescriptor];
NSMutableURLRequest *urlRequest = [self.restKitObjectManager multipartFormRequestWithObject:ad method:RKRequestMethodPOST path:path parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
// NSArray *photosArray = ad.photos[0];
// for(int i = 0; i < photosArray.count; i++) {
//
// NSString *name = [NSString stringWithFormat:@"ad[photos_attributes][%i][picture]", i];
// NSString *fileName = [NSString stringWithFormat:@"photo%i.jpg", i];
// [formData appendPartWithFileData:UIImagePNGRepresentation(photosArray[i])
// name:name
// fileName:fileName
// mimeType:@"image/jpg"];
// }
}];
RKObjectRequestOperation *operation = [self.restKitObjectManager objectRequestOperationWithRequest:urlRequest
success:^(RKObjectRequestOperation *operation, RKMappingResult *result) {
if ([self.serviceDelegate respondsToSelector:@selector(successfulRequestDidReturnObject:)])
[self.serviceDelegate successfulRequestDidReturnObject:nil];
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
if ([self.serviceDelegate respondsToSelector:@selector(requestDidFailWithError:)])
[self.serviceDelegate requestDidFailWithError:error];
}];
[self.restKitObjectManager enqueueObjectRequestOperation:operation];
[self.restKitObjectManager removeRequestDescriptor:requestDescriptor];
[self.restKitObjectManager removeResponseDescriptor:responseDescriptor];
}
- (NSDictionary *)adAttributes {
return @{
@"id" : @"_id",
@"title" : @"title",
@"price" : @"price",
@"local" : @"local",
@"description" : @"especification"
// @"categories" : @"categories",
// @"photos" : @"photos",
// @"latitude" : @"latitude",
// @"longitude" : @"longitude"
};
}
- (NSDictionary *)postAdAttributes {
return @{
@"_id" : @"id",
@"title" : @"title",
@"price" : @"price",
@"local" : @"local",
@"especification" : @"description"
// @"categories" : @"category_ids",
// @"user_id" : @"user_id",
// @"latitude" : @"latitude",
// @"longitude" : @"longitude"
};
}
- (NSDictionary *)userAttributes {
return @{
@"id" : @"_id",
@"email" : @"email",
@"name" : @"name",
@"avatar" : @"profileImageUrl",
@"phone" : @"phone",
@"password" : @"password",
@"contact_pref" : @"communicationPreference",
@"products_alerts" : @"productsAlerts"
};
}
- (NSDictionary *)postUserAttributes {
return @{
@"_id" : @"id",
@"email" : @"email",
@"name" : @"name",
@"phone" : @"phone",
@"password" : @"password",
@"password" : @"password_confirmation",
@"communicationPreference" : @"contact_pref"
};
}
@end这是我的NewAdViewController:
#import "NewAdViewController.h"
#import "Ads.h"
#import "JVUtils.h"
#import "JVWebService.h"
#import "AppDelegate.h"
@interface NewAdViewController ()
@end
@implementation NewAdViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (IBAction)signUp:(id)sender {
if (self.titleField.text.length <= 0) {
[JVUtils showMessage:@"Falta algo ae eem =D =D fdp." withTitle:@"Opa!"];
} else if (self.priceField.text.length <= 0) {
[JVUtils showMessage:@"Falta algo ae eem =D =D fdp" withTitle:@"Opa!"];
} else if (self.localField.text.length <= 0) {
[JVUtils showMessage:@"Falta algo ae eem =D =D fdp" withTitle:@"Opa!"];
} else if (self.descriptionField.text.length <= 0) {
[JVUtils showMessage:@"Falta algo ae eem =D =D fdp" withTitle:@"Opa!"];
} else {
Ads *newAd = [Ads new];
newAd.title = self.titleField.text;
newAd.price = self.priceField.text;
newAd.local = self.localField.text;
newAd.especification = self.descriptionField.text;
[[JVWebService sharedService] setServiceDelegate:self];
[[JVWebService sharedService] postAd:newAd];
}
}
- (void)successfulRequestDidReturnObject:(NSObject *)object {
[JVUtils showMessage:@"Anuncio cadastrado =D" withTitle:@"hadoukeeeen !"];
[[AppDelegate sharedDelegate] setCurrentUser:(User *)object];
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)requestDidFailWithError:(NSError *)error {
[JVUtils showMessage:error.localizedDescription withTitle:@"Errohue"];
}
-(void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
}
@end以下是服务器日志中的详细信息
开始在2015-03-03 18:06:15-0300作为JSON参数处理127.0.0.1的"/ads.json“:{”ad“=>{=>”ewewe“、”id“=>”、“=>”ew“、”价格“=>”25“,“标题”=>“titulp”}用户加载(0.6ms)选择用户。*从users.id =2的用户处,users.id限制了一个不允许的参数: id (0.2ms)开始(0.6ms)回滚,完成了10ms中422个不可处理实体(视图: 0.3ms / ActiveRecord: 1.4ms)
发布于 2015-03-03 21:41:50
状态代码422表示您的数据在某种程度上是不正确的--请求格式良好,但无法处理数据。
查看服务器上的日志,您可以看到id是空的,并且有一个错误消息Unpermitted parameters: id
您需要确定id为空的原因,并在发送请求之前纠正这一点。
https://stackoverflow.com/questions/28842017
复制相似问题