Reputation: 105
I'm trying to update the longitude and latitude in the database based on the alert id, when I send the id to the database I get the TypeError (can't cast ActionController::Parameters to integer). I noticed that when its submitted its submitting as a string, yet when I log the parameters in xCode its an integer.
-(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
Upvotes: 0
Views: 931
Reputation: 12818
Try to change
@alert = Alert.find(alert_params)
to
@alert = Alert.find(params[:id])
Upvotes: 3