问题
I am using the geocoder gem and I need to code a start and end address for a model, however I am not sure if I can accomplish this using this gem and if I can whether I am doing it correctly. If this cannot be done with the gem, is there another resource that will allow me to geocode two locations on the same form? This is an example of what I am trying to do, but it just passes the second address to be geocoded.
geocoded_by :start_address
before_validation :geocode
geocoded_by :delivery_address, :latitude => :end_latitude, :longitude => :end_longitude
before_validation :geocode
回答1:
If you look at the source it looks as though there's an options hash that would get overwritten active_record.rb and base.rb.
I figure there are two options: move your addresses into an included (joined) model (like Address
or something), or fork geocoder to have multiple options by key. The first one is easier, and solves the problem. The second one is cooler (might play with that myself as a kata).
回答2:
So what's going on is just simple ruby... if you do this:
class Question
def ask
"what would you like?"
end
def ask
"oh hai"
end
end
Question.new.ask
=> "oh hai"
The last method definition wins... so when you declare two geocoded_by
methods, the 2nd is the one that counts.
I think you're going to have to manually geocode, using the gem
before_validation :custom_geocoding
def custom_geocoding
start_result = Geocoder.search(start_address)
end_result = Geocoder.search(delivery_address)
# manually assign lat/lng for each result
end
来源:https://stackoverflow.com/questions/7510734/rails-geocoder-gem-issues