本文介绍了Rails Geocoder“未定义的方法错误”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

另一个真正的Ruby新手在这里,所以请温柔...
我正在使用alexreisner的Geocoder on rails 3.0,并且无法从Geocoder :: Results lib中调用方法。我深入了解Geocoder文档,但我无法完成一件简单的事情:从用户提交的地址中提取状态结果。因此,将加利福尼亚州旧金山123 Fake Street转变为CA或California。



(我的程序现在只会使用状态帮助确定结果,但我希望有完整的地址信息,以便在我完善应用程序后再使用,这就是为什么我要求提供完整地址的原因。)



我已经通过我在这里找到的文档和其他答案中收集到的最好的方法建立了我的模型,但以前类似于此主题的答案并没有帮助我:

  class Project< ActiveRecord :: Base 
attr_accessible:title,:address,:latitude,:longitude

belongs_to:用户

geocoded_by:地址
after_validation:fetch_coordinates

reverse_geocoded_by:latitude,:longitude do | obj,geo |
obj.state = geo.state
obj.country_code = geo.country_code
obj.address = [geo.state,geo.country_code] .join(,)
end
after_validation:reverse_geocode

end

但是这个变成了国家代码和地址的未定义错误方法。我不明白,因为Geocoder gem的文档明确提供了这些方法。我的模型似乎根本无法访问它们。 (根据记录,我的模型是能够拉纬度和经度从输入的地址,所以它似乎地理编码器的配置是否正确,而且方法geocoded_by的伟大工程。此外,它正确设置为谷歌API)。

我很确定我在这里错过了一些简单的东西。在rails控制台中,我甚至可以使用Geocoder.search(Miami,FL)并且提取所有县,州和城市的信息,但我似乎无法通过我的模型提取状态。



感谢您的帮助! 在Geocoder网站上的例子,你会看到第二个赋予块的值似乎是一个数组或其他形式的集合,所以你不能直接调用它的方法。相反,您必须首先将正确的对象拉出来:

  reverse_geocoded_by:lat,:lon do | obj,results | 
如果geo = results.first
obj.city = geo.city
obj.zipcode = geo.postal_code
obj.country = geo.country_code
end
结束


Another true newbie on Ruby here, so please be gentle...I'm working with alexreisner's Geocoder on rails 3.0 and am unable to call methods from the Geocoder::Results lib. I've pored over the Geocoder documentation, but I can't accomplish one simple thing: pull out a "state" result from a user-submitted address. So turn "123 Fake Street, San Francisco, CA" into "CA" or "California."

(My program will only use the "state" information for now to help determine results, but I'd like to have full address information to use at a later date when I refine the app, which is why I ask for a full address.)

I've set up my model via the best I have been able to gather from the documentation and other answers I've found here, but previous answers similar to this subject have not helped me:

class Project < ActiveRecord::Base
  attr_accessible :title, :address, :latitude, :longitude

  belongs_to :user

  geocoded_by :address
  after_validation :fetch_coordinates

  reverse_geocoded_by :latitude, :longitude do |obj, geo|
    obj.state = geo.state
    obj.country_code = geo.country_code
    obj.address = [geo.state, geo.country_code].join(",")
  end
  after_validation :reverse_geocode

end

But this turns up an "undefined error method" for state, country code and address. I don't understand because the documentation for the Geocoder gem clearly provides these methods. My model seems simply unable to access them. (For the record, my model IS able to pull latitude and longitude from an entered address, so it does seem Geocoder is configured correctly. And the method "geocoded_by" works great. Furthermore, it is properly set to the Google API).

I'm pretty sure I'm missing something straightforward here. In the rails console, I'm even able to use Geocoder.search("Miami, FL") and pull all county, state and city information, but I just can't seem to extract "state" via my model.

Thanks for any help!

解决方案

If you look at the examples on the Geocoder site, you'll see that the second value yielded to the block seems to be an array or some other form of collection, so you can't directly call the methods on it. Instead you first have to pull the right object out of it:

reverse_geocoded_by :lat, :lon do |obj,results|
  if geo = results.first
    obj.city    = geo.city
    obj.zipcode = geo.postal_code
    obj.country = geo.country_code
  end
end

这篇关于Rails Geocoder“未定义的方法错误”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-11 09:38