将 Rails 3 与 Geokit-rails3 位置 gem 结合使用
我正在尝试在 Rails 应用程序中使用 geokit-rails3 gem 获取地理位置信息。我有一个简单的模型:
class Location < ActiveRecord::Base
def lookup_ip_information(post_ip)
ip = post_ip
location = IpGeocoder.geocode(ip)
puts location.full_address
lat = location.lat
lng = location.lng
end
end
当我从控制器中使用 request.remote_ip
调用此方法时,它会抛出错误:未初始化的常量 Location::IpGeocoder
I am trying to use the geokit-rails3 gem for geolocation information in a rails application. I have a simple model:
class Location < ActiveRecord::Base
def lookup_ip_information(post_ip)
ip = post_ip
location = IpGeocoder.geocode(ip)
puts location.full_address
lat = location.lat
lng = location.lng
end
end
When I call this method with request.remote_ip
from my controller, it's throwing an error: uninitialized constant Location::IpGeocoder
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我刚刚遇到了这个问题,并通过输入完整内容解决了它
,这似乎为我解决了这个问题。
I just had this issue and resolved it by entering the full
and that seemed to solve it for me.
有同样的问题,并且无法使用上述建议让 IpGeocoder 在 Rails3 中工作。深入研究 gem 的 Rails3 源代码。看起来解决这个问题的一个简单方法是使用 Rails 3 的 Geokit MultiGeocoder 方法。
示例:
loc = Geokit::Geocoders::MultiGeocoder.geocode(remote.request_ip)
不要忘记在初始化程序中正确设置 MultiGeocoder - geokit_config.rb
Geokit::Geocoders::provider_order = [:google,:us]
Had the same issue and couldn't get IpGeocoder to work in Rails3 with above suggestions. Dug into the Rails3 source code for the gem. Looks like an easy way to work around this is use the Geokit MultiGeocoder method for Rails 3.
Example:
loc = Geokit::Geocoders::MultiGeocoder.geocode(remote.request_ip)
Don't forget to set up MultiGeocoder correctly in your initializer - geokit_config.rb
Geokit::Geocoders::provider_order = [:google,:us]