我正在尝试发布广告。我设置了一切,因为我认为这是正确的但是当我点击玻色子“cadastrar anuncio”时它返回此错误“得到422”。已经研究过这个错误然而根本无法修复。 这里有人经历过这个吗?
这是我的网络服务
#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开始发布“/ads.json”for 127.0.0.1 AdsController处理#创建为JSON参数: {“ad”=&gt; {“description”=&gt;“ewewe”,“id”=&gt;“”,“local”=&gt;“ew”, “price”=&gt;“25”,“title”=&gt;“titulp”}}用户负载(0.6ms)SELECT用户。* FROM users WHERE users.id = 2 ORDER BY users.id ASC LIMIT 1 未允许的参数:id(0.2ms)BEGIN(0.6ms)ROLLBACK已完成 422个不可处理的实体,10ms(视图:0.3ms | ActiveRecord:1.4ms)
答案 0 :(得分:0)
状态代码422表示您的数据在某种程度上不正确 - 请求格式正确但数据无法处理。
从服务器查看日志,您可以看到id
为空并且出现错误消息Unpermitted parameters: id
您需要确定id为空的原因并在发送请求之前更正此错误。