1

我有一个将 JSON 数据发送到 Ruby on Rails API 的 IOS 应用程序。在 Rails 方面,如果我在本地进行测试,我会看到参数通过就好了。当我在 Heroku 上调用生产服务器的 IOS 应用程序时,参数是 nil 对象。

这是IOS代码

NSDictionary *noteVo = [NSDictionary dictionaryWithObjectsAndKeys:note->title, @"title", note->body, @"body", note->note_id, @"note_id", nil];

    NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:[_dm getUser]->uuid, @"uuid", noteVo, @"note", nil];

    NSError *writeError = nil;

    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictionary options:NSJSONWritingPrettyPrinted error:&writeError];

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:
                                    [NSURL URLWithString:[BASE_URL stringByAppendingString:@"note/update"]] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
    [request setHTTPMethod:@"POST"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [request setValue:[NSString stringWithFormat:@"%d", [jsonData length]] forHTTPHeaderField:@"Content-Length"];
    [request setHTTPBody: jsonData];

    [[NSURLConnection alloc] initWithRequest:request delegate:self];

这是导轨代码

def update_note

    puts "debug note update #{ params[:note] }"

    uuid = params[:uuid]
    user = User.find_by_uuid(uuid)
    noteid = params[:note][:note_id] if params[:note][:note_id]

    if !user.nil?
      note = nil
        note = user.notes.find(noteid) if !noteid.nil?
        if !note.nil?
            user.notes.find(noteid) 
            note.update_attributes!(params[:note])
        else
            note = user.notes.build(params[:note])
        note.save
        end
        render :json => {:result => note}
    else
        render :json => {:error => "nope"}
    end
  end
4

0 回答 0