我的代码在控制台中运行,但在 App 中运行
➜ Meet-and-Eat git:(master) ✗ rails c
Running via Spring preloader in process 15789
Loading development environment (Rails 5.2.2)
2.5.3 :001 > i = ["10 Palmerston Street", "DERBY"]
=> ["10 Palmerston Street", "DERBY"]
2.5.3 :002 > result = Geocoder.search("#{i[0]}, #{i[1]}").first.coordinates
=> [52.9063415, -1.4937474]
我的代码:
<% @places = [] %>
<% @placesCoordinations = [] %>
<% @information.each do |i| %>
<% @places.push([i.address1, i.town, i.postcode, information_path(i)]) %>
<% end %>
<% @places.each do |i| %>
<% result = Geocoder.search("#{i[0]}, #{i[1]}").first.coordinates %>
<% @placesCoordinations.push(result) %>
<% end %>
错误 :
NoMethodError in Information#full_map_adresses.
Showing /Users/mateuszstacel/Desktop/Meet-and-Eat/app/views/information/full_map_adresses.html.erb where line #10 raised:
undefined method `coordinates' for nil:NilClass
<% @places.each do |i| %>
<% result = Geocoder.search("#{i[0]}, #{i[1]}").first.coordinates%> //this line is breaking my app
<% @placesCoordinations.push(result) %>
<% end %>
但是,如果我只使用一个有效的位置或邮政编码或街道地址,但我需要同时使用它们以提高精度。
<% @places = [] %>
<% @placesCoordinations = [] %>
<% @information.each do |i| %>
<% @places.push([i.address1, i.town, i.postcode, information_path(i)]) %>
<% end %>
<% @places.each do |i| %>
<% result = Geocoder.search("#{i[2]}").first.coordinates %>
<% @placesCoordinations.push(result) %>
<% end %>