【问题标题】:Validating longitude / latitude with geocoder gem使用地理编码器 gem 验证经度/纬度
【发布时间】:2013-04-03 11:50:15
【问题描述】:

我关注了这个Geocoder Railscast,我正在玩弄它,看看我是否可以向 Geocoder Gem 返回的经度和纬度坐标添加验证,以防用户输入不返回任何经度和纬度的错误地址坐标。

我能够使用下面的代码完成这项工作,但是当我输入正确的地址时,它会在用户第一次提交表单时给他们一个验证错误,即使返回的经度和纬度坐标也是如此。用户第二次提交表单时它起作用了。

我怀疑这是因为当用户提交表单时没有经度和纬度坐标,但在地理编码器 gem 返回坐标时第二次出现。

我的代码有问题还是我应该以不同的方式处理这个问题?

型号

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

终端输出

=> 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

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 validation google-maps rails-geocoder


    【解决方案1】:

    为什么您不能在验证之前对位置进行地理编码? :address_changed?如果这些字段中的任何一个已更改,即使对于新记录也将返回 true。验证它,如果它返回无效的纬度/经度值,那么您的 :latitude 存在验证将失败。

    只需将 after_validation 更改为 before_validation

    【讨论】:

    • 感谢菲利普,当它盯着我的脸时,我在错误的地方寻找解决方案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多