TypeError(不能将ActionController :: Parameters转换为整数)

时间:2015-04-01 14:13:52

标签: objective-c ruby-on-rails

我试图根据警报ID更新数据库中的经度和纬度,当我将id发送到数据库时,我得到了TypeError(无法将ActionController :: Parameters转换为整数) 。我注意到当它提交它作为字符串提交时,但是当我在xCode中记录参数时它是一个整数。

-(void)updateCordinates{

    NSString *alertID = [defaults objectForKey:@"alertID"];

    NSInteger alert_id = [alertID
                          integerValue];

    NSDictionary *userLoc = [[NSUserDefaults standardUserDefaults] objectForKey:@"userLocation"];
    NSString *latitude = [userLoc objectForKey:@"lat"];
    NSString *longitude = [userLoc objectForKey:@"long"];

    // Create the request.
    request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://localhost:3000/api/v1/alerts/%ld/",(long)alert_id]]];

    NSLog(@"%@", request);


    // Specify that it will be a PUT request
    request.HTTPMethod = @"PUT";

    // This is how we set header fields
    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];

    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

    // Convert your data and set your request's HTTPBody property
    NSDictionary *parameters = [[NSDictionary alloc] initWithObjectsAndKeys:
                                [NSNumber numberWithInteger:alert_id], @"id",
                                latitude, @"latitude",
                                longitude, @"longitude",
                                nil];
    NSLog(@"%@", parameters);
    NSError *error;
    NSData *postdata = [NSJSONSerialization dataWithJSONObject:parameters options:0 error:&error];
    [request setHTTPBody:postdata];

    // Create url connection and fire request
    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    [connection start];

}

module Api
  module V1
    class AlertsController < ApplicationController
      respond_to :json
      wrap_parameters  include: [:user_id,:longitude, :latitude, :id]
      skip_before_filter  :verify_authenticity_token
      def alert_params
          params.require(:alert).permit(:user_id,:longitude, :latitude, :id)
      end
      def new
        @alert = Alert.new(alert_params)
      end
      def create
        @alert = Alert.create(alert_params)
        respond_to do |format|
            if @alert.save
              format.json { render json: @alert.id}
            else
              format.json { render json: @alert.errors, status: :unprocessable_entity }
            end
        end
      end
      def edit
        @alert = Alert.find(alert_params)
      end
      def update
        @alert = Alert.find(alert_params)

        respond_to do |format|
            if @alert.update_attributes(alert_params)
              format.json { render json: "Alert Saved"}
            else
              format.json { render json: @alert.errors, status: :unprocessable_entity }
            end
        end
      end
      def getalerts

        alerts = Alert.where(:status => "active")
        alertArray = Array.new()

        alerts.each do |alert|

          distance = triangulate(alert.latitude, alert.longitude, params[:latitude], params[:longitude])

          if distance < params[:radius].to_f
            testHash = Hash.new()
            testHash = { "username" =>alert.user.username,"latitude" => alert.latitude, "longitude" => alert.longitude  }
            alertArray.push(testHash)
          end
        end
        respond_to do |format|
          format.json { render json: alertArray}      
        end
      end  
    end
  end
end         

1 个答案:

答案 0 :(得分:2)

尝试更改

@alert = Alert.find(alert_params)

@alert = Alert.find(params[:id])