从iOS客户端返回状态422到PATCH到Rails应用 [英] PATCH to Rails app from iOS Client Returning Status 422

查看:138
本文介绍了从iOS客户端返回状态422到PATCH到Rails应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有 Business 模型对象且计数为访问的Rails应用程序。我有一个iOS客户端,它使用AFNetworking 2.0与该Rails应用程序进行交互,并且当用户在应用程序中点按业务时,它将PATCH发送到Rails应用程序,从而增加访问次数从事这项业务。但是,我从服务器返回了状态码422无法处理的实体错误,并且服务器上的业务访问次数的计数没有增加。

I have a Rails app with a Business model object with a count of visits. I have an iOS client that interacts with this Rails app using AFNetworking 2.0, and when a user taps a business in the app, it sends a PATCH to the Rails app, incrementing visits for that business. However, I'm getting a status code 422 "Unprocessable Entity" error back from the server, and the business's visits count on the server is not getting incremented.

- (NSURLSessionDataTask *)visitVenue:(NOTVenue *)venue withCompletionBlock:(void (^)(BOOL success))completionBlock {
NSURLSessionDataTask *dataTask = [self PATCH:[NSString stringWithFormat:@"businesses/%li.json", (long)[venue.identifier integerValue]]
                                  parameters:@{@"visits": @(venue.numberOfVisits + 1)}
                                     success:^(NSURLSessionDataTask *task, id responseObject) {
                                         NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)task.response;

                                         if (httpResponse.statusCode == 200) {
                                             dispatch_async(dispatch_get_main_queue(), ^{
                                                 completionBlock(YES);
                                             });
                                         } else {
                                             dispatch_async(dispatch_get_main_queue(), ^{
                                                 completionBlock(NO);
                                             });
                                         }
                                     }
                                     failure:^(NSURLSessionDataTask *task, NSError *error) {
                                         dispatch_async(dispatch_get_main_queue(), ^{
                                             completionBlock(NO);
                                         });
                                     }];

return dataTask;

}

# PATCH/PUT /businesses/1
# PATCH/PUT /businesses/1.json
def update
  respond_to do |format|
    if @business.update(business_params)
      format.html { redirect_to @business, notice: 'Business was successfully updated.' }
      format.json { head :no_content }
    else
      format.html { render action: 'edit' }
      format.json { render json: @business.errors, status: :unprocessable_entity }
    end
  end
end


推荐答案

为此找到了正确的解决方案 Rails API网站。下面是代码:

The correct solution for this was found on the Rails API site. Here is the code:

class ApplicationController < ActionController::Base
  protect_from_forgery
  skip_before_action :verify_authenticity_token, if: :json_request?

  protected

  def json_request?
    request.format.json?
  end
end

这篇关于从iOS客户端返回状态422到PATCH到Rails应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆