看完RailsCasts#273后我想使用Geocoder宝石.我见过这个:
class Skatepark < ActiveRecord::Base
reverse_geocoded_by :latitude, :longitude
after_validation :fetch_address
...
end
Run Code Online (Sandbox Code Playgroud)
这将反转地理编码坐标并填充:addressformatted_address.
我可以得到这个分离为:street,:locality,:region,:country,和:postal_code地址解析器的宝石?
在我的sesions表中我使用请求方法存储国家和城市名称,现在,在ubuntu服务器中它运行良好但在redhat中我收到此错误
NoMethodError (undefined method `city' for nil:NilClass)
Run Code Online (Sandbox Code Playgroud)
我尝试过这个问题的解决方案,我重新启动了我的服务器,但它对我不起作用.
在两台服务器中,我使用的是Passenger + Apache
我在 /new.html.erb 中有这个表格:
<%= form_for @vendor, multipart: true do |f| %>
<%= f.text_field :name, "Store Name" %>
<%= f.text_field :address, "Store Address" %>
<%= f.file_field :image %>
<%= f.submit "Save", class: "btn btn-success" %>
<% end %>
Run Code Online (Sandbox Code Playgroud)
和这些供应商方法:
def new
@vendors = Vendor.all
@vendor = Vendor.new
end
def vendor_params
params.require(:vendor).permit(:id, :name, :latitude, :longitude, :address, :image)
end
Run Code Online (Sandbox Code Playgroud)
当我尝试渲染页面时,出现此错误:
undefined method `merge' for "Store Name":String
Extracted source (around line #8):
5: <h4>New Vendor Form</h4>
6:
7: <%= form_for @vendor, :html => {:multipart …Run Code Online (Sandbox Code Playgroud) 因此,我使用Geocoder根据用户在提交表单时提供的地址提取纬度和经度坐标.我这样做是为了让我可以使用Google Maps API绘制标记.这在开发中非常有效 - 零问题.然而,当我推向生产时,Geocoder不会生成纬度和经度.我检查了我的生产日志,没有错误.我还检查确保gem不仅仅是为了开发安装而且我已经在我的生产机器上重新启动了unicorn和nginx.关于可能发生什么的任何想法?
这是我正在执行此任务的模型的模型代码 -
class Order < ActiveRecord::Base
attr_accessible :city, :customer_email, :customer_id, :delivery_date, :delivery_time, :street, :zipcode, :coupon, :total, :name, :items_with_day, :user_id, :shopping_cart_id, :extras, :latitude, :longitude, :location_name, :phone, :suite, :state
belongs_to :user
validates :name, :presence => true
validates :street, :presence => true
validates :city, :presence => true
validates :zipcode, :presence => true
def address
[street, city, state, "United States"].compact.join(', ')
end
geocoded_by :address
after_validation :geocode
end
Run Code Online (Sandbox Code Playgroud)
============更新=============
所以在提出建议后,我在生产机器上打开了rails控制台 -
bundle exec rails console production
Run Code Online (Sandbox Code Playgroud)
跑了 - …
我在我的应用程序中使用了Geocoder和Sunspot gem,我有一个叫做的字段:search_near_address,假设用户能够输入他们想在附近搜索的地址X amount of miles.我想要映射到的是我:address用于该:search_near_address领域的商店.这样,用户可以在:search_near_address现场输入地址(即451 University Avenue,Palo Alto,CA),它将在半径50英里的范围内搜索.
太阳黑子1.2.1
class Store < ActiveRecord::Base
attr_accessible :address, :latitude, :longitude
has_many :products
geocoded_by :address
after_validation :geocode
reverse_geocoded_by :latitude, :longitude
after_validation :reverse_geocode
end
Run Code Online (Sandbox Code Playgroud)
class Product < ActiveRecord::Base
belongs_to :store
searchable do # Searching with product model.
string :search_near # For rake sunspot:reindex
location :location
end
def search_near_address
store.address if store # You may have to use the "if store".
end …Run Code Online (Sandbox Code Playgroud) 我想替换开发环境中的127.0.0.1 IP,以便手动测试地理编码器gem.我怎样才能做到这一点 ?
我想是的,但它不工作.我遇到以下错误:
.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.0.0.rc1/lib/active_support/infle??ctor/methods.rb:226:in 'const_get': uninitialized constant SpoofIp (NameError)
Run Code Online (Sandbox Code Playgroud) development-environment ruby-on-rails ip-address rails-geocoder
我想实现这样的语言会动态地理编码前设置和/或反地理编码时,现在,我在应用程序的初始化设置的语言.
Geocoder.configure(
:units => :km,
:language => :en
)
Run Code Online (Sandbox Code Playgroud)
我希望它能动态设置语言,例如:
Geocoder.configure(
:units => :km,
:language => lambda {|something| I18n.locale}
)
Run Code Online (Sandbox Code Playgroud)
有谁知道如何实现这一目标?
(Ruby 1.9.3,MongoDB 2.0.4,Rails 3.2,Mongoid 2.4,Geocoder 1.1.1)
我有以下型号:
class Company
include Mongoid::Document
embeds_one :office
index [[ "office.coordinates", Mongo::GEO2D ]]
end
class Office
include Mongoid::Document
include Geocoder::Model::Mongoid
geocoded_by :address
field :city, :type => String
field :state, :type => String
field :coordinates, :type => Array
embedded_in :company
after_validation :geocode
def address
"#{city}, #{state}"
end
end
Run Code Online (Sandbox Code Playgroud)
我在控制台中这样做:
> c = Company.new
=> #<Company _id: 4f885aa56d20f03898000003, _type: nil>
> c.office = Office.new(:city => "San Francisco", :state => "CA")
=> #<Office _id: 4f885ab66d20f03898000004, _type: nil, city: …Run Code Online (Sandbox Code Playgroud) 我的数据库中有一个地址表已有一段时间了,最近又遇到了Geocoder的宝石(甜蜜的宝石).无论如何,我已经编辑了我的模型以包括纬度和经度列,以及指定每条记录应该是什么geocoded_by,所以现在我想简单地对表格中的所有地址进行地理编码,现在这些地址目前只有零纬度和经度.
我以为我已经通过rails控制台想出来了,只需运行命令:
Address.all.each{ |address| address.geocode }
Run Code Online (Sandbox Code Playgroud)
这将返回带有填充纬度和经度列的每个条目,但是在之后检查各个元素时 - 它们仍然具有零纬度和经度(即记录实际上没有被更新).我想也许address.geocode!会奏效,但那回来了NoMethodError.
这必须是一个简单的,但似乎我只是缺乏经验,不知道它是什么... aaaand更多的修补我想通了:
解
Address.all.each{ |address| address.update_attributes(latitude: address.geocode[0], longitude: address.geocode[1]) }
Run Code Online (Sandbox Code Playgroud) 如果已达到API查询限制,如何询问Geocoder gem地址?
csv.each_with_index do |row, i|
if !row[:location_1].nil?
coords = row[:location_1]
g = Geocoder.address(coords)
row[:address] = g
end
if g.include?('over query limit')
p "limit hit"
end
end
Run Code Online (Sandbox Code Playgroud)
我不确定要问哪个对象.控制台中出现错误:
Google Geocoding API error: over query limit.
Run Code Online (Sandbox Code Playgroud)
但我不知道如何在代码中检查它.有帮助吗?
rails-geocoder ×10
ruby-on-rails ×10
ruby ×5
forms ×1
google-maps ×1
ip-address ×1
mongoid ×1
sunspot ×1