简体   繁体   English

使用Geocoder gem验证经度/纬度

[英]Validating longitude / latitude with geocoder gem

I followed this Geocoder Railscast & I'm playing around with it to see if I can add validation to the longitude & latitude coordinates returned by the Geocoder Gem incase the user inputs a faulty address that does not return any longitude & latitude coordinates. 我遵循了此Geocoder Railscast ,并且正在研究它是否可以添加验证到Geocoder Gem返回的经度和纬度坐标,以防用户输入错误的地址而不返回任何经度和纬度坐标。

I was able to get this working to a point with the code below but when I enter a correct address it gives the user a validation error the first time they submit the form, even though longitude & latitude coordinates where returned. 我可以使用下面的代码来解决这个问题,但是当我输入正确的地址时,即使返回了经度和纬度坐标,也给用户首次提交表单时出现了验证错误。 The second time the user submits the form it works. 用户第二次提交它起作用的表单。

I suspect this is happening as when the user submits the form there are no longitude & latitude coordinates but there are the second time round as the geocoder gem returned the coordinates. 我怀疑这是由于用户提交表单时没有经度和纬度坐标而发生的,而是随着Geocoder gem返回坐标第二次出现。

Is there something wrong with my code or should I be approaching this differently? 我的代码有什么问题吗?还是应该以不同的方式处理呢?

Model 模型

class Location < ActiveRecord::Base
  attr_accessible :address1, 
                  :country, 
                  :latitude, 
                  :longitude, 
                  :name

  validates :latitude, :presence => {message: "Not a valid location on Google Maps, please check name address & country fields" }

  geocoded_by :address
  after_validation :geocode, :if => :address_changed?

  def address
  [name, address1, country].compact.join(' ')
    end

    def address_changed?
  attrs = %w(name address1 country)
  attrs.any?{|a| send "#{a}_changed?"}
    end
end

Terminal OutPut 终端输出

=> Booting WEBrick
=> Rails 3.2.12 application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
[2013-04-02 02:03:41] INFO  WEBrick 1.3.1
[2013-04-02 02:03:41] INFO  ruby 1.9.3 (2013-02-22) [x86_64-linux]
[2013-04-02 02:03:41] INFO  WEBrick::HTTPServer#start: pid=4316 port=3000


Started POST "/locations" for 127.0.0.1 at 2013-04-02 02:04:04 +0100
Processing by LocationsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"4HUay+IOgA7JnCSg8ZZ0zVMcHpj7djUPlfqe1emTMSY=", "location"=>{"name"=>"Louch Dan", "address1"=>"Roundwood", "town"=>"", "county"=>"", "state"=>"", "country"=>"Ireland", "latitude"=>"", "longitude"=>""}, "commit"=>"Create Location"}
   (0.1ms)  begin transaction
   (0.1ms)  rollback transaction
  Rendered locations/_form.html.erb (8.9ms)
  Rendered locations/new.html.erb within layouts/application (63.4ms)
Completed 200 OK in 410ms (Views: 156.1ms | ActiveRecord: 6.0ms)


Started GET "/assets/application.css?body=1" for 127.0.0.1 at 2013-04-02 02:04:06 +0100
Served asset /application.css - 304 Not Modified (11ms)
[2013-04-02 02:04:06] WARN  Could not determine content-length of response body. Set content-length of the response or set Response#chunked = true


Started GET "/assets/jquery_ujs.js?body=1" for 127.0.0.1 at 2013-04-02 02:04:06 +0100
Served asset /jquery_ujs.js - 304 Not Modified (1ms)
[2013-04-02 02:04:06] WARN  Could not determine content-length of response body. Set content-length of the response or set Response#chunked = true


Started GET "/assets/jquery.js?body=1" for 127.0.0.1 at 2013-04-02 02:04:06 +0100
Served asset /jquery.js - 304 Not Modified (7ms)


Started GET "/assets/scaffolds.css?body=1" for 127.0.0.1 at 2013-04-02 02:04:06 +0100
Served asset /scaffolds.css - 304 Not Modified (1ms)


Started GET "/assets/locations.css?body=1" for 127.0.0.1 at 2013-04-02 02:04:06 +0100
Served asset /locations.css - 304 Not Modified (1ms)
[2013-04-02 02:04:06] WARN  Could not determine content-length of response body. Set content-length of the response or set Response#chunked = true
[2013-04-02 02:04:06] WARN  Could not determine content-length of response body. Set content-length of the response or set Response#chunked = true


Started GET "/assets/locations.js?body=1" for 127.0.0.1 at 2013-04-02 02:04:06 +0100
Served asset /locations.js - 304 Not Modified (4ms)
[2013-04-02 02:04:06] WARN  Could not determine content-length of response body. Set content-length of the response or set Response#chunked = true
[2013-04-02 02:04:06] WARN  Could not determine content-length of response body. Set content-length of the response or set Response#chunked = true


Started GET "/assets/application.js?body=1" for 127.0.0.1 at 2013-04-02 02:04:06 +0100
Served asset /application.js - 304 Not Modified (7ms)
[2013-04-02 02:04:06] WARN  Could not determine content-length of response body. Set content-length of the response or set Response#chunked = true


Started POST "/locations" for 127.0.0.1 at 2013-04-02 02:04:22 +0100
Processing by LocationsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"4HUay+IOgA7JnCSg8ZZ0zVMcHpj7djUPlfqe1emTMSY=", "location"=>{"name"=>"Louch Dan", "address1"=>"Roundwood", "town"=>"", "county"=>"", "state"=>"", "country"=>"Ireland", "latitude"=>"53.07004130000001", "longitude"=>"-6.2804327"}, "commit"=>"Create Location"}
   (0.1ms)  begin transaction
  SQL (15.3ms)  INSERT INTO "locations" ("address1", "country", "county", "created_at", "latitude", "longitude", "name", "state", "town", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)  [["address1", "Roundwood"], ["country", "Ireland"], ["county", ""], ["created_at", Tue, 02 Apr 2013 01:04:22 UTC +00:00], ["latitude", 53.07004130000001], ["longitude", -6.2804327], ["name", "Louch Dan"], ["state", ""], ["town", ""], ["updated_at", Tue, 02 Apr 2013 01:04:22 UTC +00:00]]
   (71.9ms)  commit transaction
Redirected to http://0.0.0.0:3000/locations/10
Completed 302 Found in 236ms (ActiveRecord: 87.3ms)


Started GET "/locations/10" for 127.0.0.1 at 2013-04-02 02:04:22 +0100
Processing by LocationsController#show as HTML
  Parameters: {"id"=>"10"}
  Location Load (3.8ms)  SELECT "locations".* FROM "locations" WHERE "locations"."id" = ? LIMIT 1  [["id", "10"]]
  Rendered locations/show.html.erb within layouts/application (9.8ms)
Completed 200 OK in 25ms (Views: 15.5ms | ActiveRecord: 3.8ms)


Started GET "/assets/application.css?body=1" for 127.0.0.1 at 2013-04-02 02:04:22 +0100
Served asset /application.css - 304 Not Modified (0ms)
[2013-04-02 02:04:22] WARN  Could not determine content-length of response body. Set content-length of the response or set Response#chunked = true


Started GET "/assets/locations.css?body=1" for 127.0.0.1 at 2013-04-02 02:04:22 +0100
Served asset /locations.css - 304 Not Modified (0ms)
[2013-04-02 02:04:22] WARN  Could not determine content-length of response body. Set content-length of the response or set Response#chunked = true


Started GET "/assets/jquery_ujs.js?body=1" for 127.0.0.1 at 2013-04-02 02:04:22 +0100
Served asset /jquery_ujs.js - 304 Not Modified (0ms)


Started GET "/assets/jquery.js?body=1" for 127.0.0.1 at 2013-04-02 02:04:22 +0100
Served asset /jquery.js - 304 Not Modified (6ms)
[2013-04-02 02:04:22] WARN  Could not determine content-length of response body. Set content-length of the response or set Response#chunked = true
[2013-04-02 02:04:22] WARN  Could not determine content-length of response body. Set content-length of the response or set Response#chunked = true


Started GET "/assets/scaffolds.css?body=1" for 127.0.0.1 at 2013-04-02 02:04:22 +0100
Served asset /scaffolds.css - 304 Not Modified (0ms)
[2013-04-02 02:04:22] WARN  Could not determine content-length of response body. Set content-length of the response or set Response#chunked = true


Started GET "/assets/application.js?body=1" for 127.0.0.1 at 2013-04-02 02:04:22 +0100
Served asset /application.js - 304 Not Modified (43ms)


Started GET "/assets/locations.js?body=1" for 127.0.0.1 at 2013-04-02 02:04:22 +0100
Served asset /locations.js - 304 Not Modified (0ms)
[2013-04-02 02:04:22] WARN  Could not determine content-length of response body. Set content-length of the response or set Response#chunked = true
[2013-04-02 02:04:22] WARN  Could not determine content-length of response body. Set content-length of the response or set Response#chunked = true

Why can't you :geocode the Location before validation? 为什么不能在验证之前对地理位置进行地理编码? :address_changed? :address_changed? will return true even for new records if any of those fields have changed. 如果这些字段中的任何一个已更改,即使对于新记录也将返回true。 Validate it and if it returns invalid lat/lng values then your :latitude presence validation will fail. 验证它,如果它返回无效的lat / lng值,则您的:latitude在线状态验证将失败。

Just change that after_validation to before_validation . 只需将after_validation更改为before_validation

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Geocoder Gem验证经度/纬度 - Validating Longitude/Latitude with Geocoder Gem 从rails-geocoder gem中的控制器获取纬度和经度值 - getting latitude and longitude values from controller in rails-geocoder gem 通过街道地址,通过Geocoder gem地理编码更新经度/纬度 - Update longitude / latitude with geocoder gem geocode by street address 使用geocoder gem查找经度和纬度附近的学校附近的学校 - Find Near banks , schools from latitude and longitude using geocoder gem 红宝石地理编码器的宝石是否会消耗提供商规定的配额以获取经度/纬度和国家/地区代码? - Does ruby geocoder gem consume quota imposed by providers to get latitude/longitude and country code? Ruby Geocoder不更新纬度/经度Rails 4 - Ruby Geocoder not updating latitude/longitude Rails 4 纬度/经度在Ruby的EXIFR宝石中无法正常显示 - Latitude/Longitude not woking in EXIFR gem for Ruby Geocoder寻找经纬度栏位,但不存在 - Geocoder looking for latitude and longitude fields but they don't exist 我们可以在使用gecoder宝石时检查经度和纬度的唯一性吗? - Can we check the uniqueness of latitude and longitude while using gecoder gem? Geocoder vs Geokit用于在纬度/经度坐标的X英里内查找对象? - Geocoder vs Geokit for finding objects within X miles of a latitude/longitude coordinate?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM