-
-
Save tomciopp/112cc28b7d52402079ad to your computer and use it in GitHub Desktop.
Geocoding multiple addresses
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#This is what I have in the model | |
before_validation :custom_geocoding | |
def custom_geocoding | |
start_result = Geocoder.search(shipping_address) | |
end_result = Geocoder.search(delivery_address) | |
# manually assign lat/lng for each result | |
self.shipping_latitude = start_result.latitude | |
self.shipping_longitude = start_result.longitude | |
self.delivery_latitude = end_result.latitude | |
self.delivery_longitude = end_result.longitude | |
end | |
#This is the resulting error message that I get when I try to create a post: | |
NoMethodError in LoadsController#create | |
undefined method `latitude' for #<Array:0x00000105f85a88> | |
Rails.root: /Users/thomascioppettini/rails_projects/want-freight | |
Application Trace | Framework Trace | Full Trace | |
app/models/load.rb:12:in `custom_geocoding' | |
app/controllers/loads_controller.rb:47:in `block in create' | |
app/controllers/loads_controller.rb:46:in `create' | |
Request | |
Parameters: | |
{"utf8"=>"✓", | |
"authenticity_token"=>"W2nwQFFRrqLi7AJkHHWpUTVL7avzKCH9yL+Q4tp/PMM=", | |
"load"=>{"shipping_address"=>"1834 Wake Forest Rd, | |
Winston Salem, | |
NC", | |
"shipping_date"=>"2011-10-01", | |
"shipping_time(1i)"=>"", | |
"shipping_time(2i)"=>"", | |
"shipping_time(3i)"=>"", | |
"shipping_time(4i)"=>"10", | |
"shipping_time(5i)"=>"30", | |
"delivery_address"=>"1 Infinite Loop, | |
Cupertino, | |
CA", | |
"delivery_date"=>"2011-10-02", | |
"delivery_time(1i)"=>"", | |
"delivery_time(2i)"=>"", | |
"delivery_time(3i)"=>"", | |
"delivery_time(4i)"=>"12", | |
"delivery_time(5i)"=>"00", | |
"truck_type"=>"1", | |
"notes"=>""}, | |
"commit"=>"Create Load"} | |
Show session dump | |
Show env dump | |
Response | |
Headers: | |
none | |
#I do not get any errors when I am just trying to geocode one address and the post creates fine when I do it that way. | |
#Here is the create action in my controller: | |
before_filter :authenticate_user! | |
def create | |
@load = current_user.loads.build(params[:load]) | |
respond_to do |format| | |
if @load.save | |
format.html { redirect_to @load, notice: 'Load was successfully created.' } | |
format.json { render json: @load, status: :created, location: @load } | |
else | |
format.html { render action: "new" } | |
format.json { render json: @load.errors, status: :unprocessable_entity } | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment