【问题标题】:Geokit in Ruby on Rails, problem with acts_as_mappableRuby on Rails 中的 Geokit,acts_as_mappable 的问题
【发布时间】:2009-11-15 21:05:09
【问题描述】:

我查看了相关问题列表,但我的问题似乎没有列出,因此这里是:

基本上我正在尝试在 Ruby on Rails 环境中使用 Geokit,我不确定我是否正确安装了它,我已将它包含在 environment.rb 中(并完成了 rake db:install),我现在正在尝试执行以下操作:

require 'active_record'
require 'geokit'

class Store < ActiveRecord::Base
    acts_as_mappable
end

不幸的是,当我尝试运行它并查看它是否正常时,我收到以下错误:

c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record/base.rb:1959:in `method_missing': undefined local variable or method `acts_as_mappable' for #<Class:0x4cd261c> (NameError)
    from C:/Users/Erika/Documents/Visual Studio 2008/Projects/StoreLocator/StoreLocator/app/models/store.rb:5

我正在为 Visual Studio 2008 运行 Ruby in Steel,我不确定我做错了什么,因为我发现的所有在线教程都比较陈旧并且适用于我。任何帮助将不胜感激。谢谢!

编辑:(根据本的要求添加) 以下是我的 environment.rb 的样子

# Be sure to restart your server when you modify this file

# Specifies gem version of Rails to use when vendor/rails is not present
RAILS_GEM_VERSION = '2.3.4' unless defined? RAILS_GEM_VERSION

# Bootstrap the Rails environment, frameworks, and default configuration
require File.join(File.dirname(__FILE__), 'boot')

Rails::Initializer.run do |config|
  # Settings in config/environments/* take precedence over those specified here.
  # Application configuration should go into files in config/initializers
  # -- all .rb files in that directory are automatically loaded.

  # Add additional load paths for your own custom dirs
  # config.load_paths += %W( #{RAILS_ROOT}/extras )

  # Specify gems that this application depends on and have them installed with rake gems:install
  # config.gem "bj"
  # config.gem "hpricot", :version => '0.6', :source => "http://code.whytheluckystiff.net"
  # config.gem "sqlite3-ruby", :lib => "sqlite3"
  # config.gem "aws-s3", :lib => "aws/s3"
  config.gem "geokit"
  config.gem "ym4r"

  # Only load the plugins named here, in the order given (default is alphabetical).
  # :all can be used as a placeholder for all plugins not explicitly named
  # config.plugins = [ :exception_notification, :ssl_requirement, :all ]

  # Skip frameworks you're not going to use. To use Rails without a database,
  # you must remove the Active Record framework.
  # config.frameworks -= [ :active_record, :active_resource, :action_mailer ]

  # Activate observers that should always be running
  # config.active_record.observers = :cacher, :garbage_collector, :forum_observer

  # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
  # Run "rake -D time" for a list of tasks for finding time zone names.
  config.time_zone = 'UTC'

  # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
  # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}')]
  # config.i18n.default_locale = :de
end

# These defaults are used in GeoKit::Mappable.distance_to and in acts_as_mappable
GeoKit::default_units = :kms
GeoKit::default_formula = :sphere

# This is the timeout value in seconds to be used for calls to the geocoder web
# services.  For no timeout at all, comment out the setting.  The timeout unit
# is in seconds. 
#GeoKit::Geocoders::timeout = 3

# These settings are used if web service calls must be routed through a proxy.
# These setting can be nil if not needed, otherwise, addr and port must be 
# filled in at a minimum.  If the proxy requires authentication, the username
# and password can be provided as well.
GeoKit::Geocoders::proxy_addr = nil
GeoKit::Geocoders::proxy_port = nil
GeoKit::Geocoders::proxy_user = nil
GeoKit::Geocoders::proxy_pass = nil

# This is your yahoo application key for the Yahoo Geocoder.
GeoKit::Geocoders::yahoo = 'REPLACE_WITH_YOUR_YAHOO_KEY'

# This is your Google Maps geocoder key. 
GeoKit::Geocoders::google = 'apikey'

# This is your username and password for geocoder.us.
# To use the free service, the value can be set to nil or false.  For 
# usage tied to an account, the value should be set to username:password.
GeoKit::Geocoders::geocoder_us = false 

# This is your authorization key for geocoder.ca.
# To use the free service, the value can be set to nil or false.  For 
# usage tied to an account, set the value to the key obtained from
# Geocoder.ca.

GeoKit::Geocoders::geocoder_ca = false

# This is the order in which the geocoders are called in a failover scenario
# If you only want to use a single geocoder, put a single symbol in the array.
# Valid symbols are :google, :yahoo, :us, and :ca.
# Be aware that there are Terms of Use restrictions on how you can use the 
# various geocoders.  Make sure you read up on relevant Terms of Use for each
# geocoder you are going to use.
GeoKit::Geocoders::provider_order = [:google]

【问题讨论】:

    标签: ruby-on-rails activerecord geokit


    【解决方案1】:

    acts_as_mappable 是 geokit-rails 的一部分。您需要安装 geokit-rails 插件。

    script/plugin install git://github.com/andre/geokit-rails.git
    

    要检查插件是否正确安装,请查看 Rails 应用程序的 vendor/plugins 目录。它应该有一个 geokit-rails 子目录。它,你会找到所有的插件文件,其他子目录,包括文件acts_as_mappable.rb(在vendor\plugins\geokit-rails\lib\geokit-rails)。

    如果一切似乎都已正确安装,请尝试将"require geokit" 添加到插件根文件夹 (vendor\plugins\geokit-rails) 中的 init.rb 文件的顶部。

    请务必在进行修改后重新启动您的应用服务器。

    【讨论】:

    • 天哪,你是对的。我用的是插件,没想到gem的作用。仍然对require "active_record" 业务感到困惑。
    • 当我安装它时,它告诉我它已经安装了,除了安装它我应该做其他事情吗?可能会将它与其他地理套件混淆吗?非常感谢迄今为止对双方的帮助
    • 试了又试,肯定有问题。当我尝试安装上述插件时,当我使用 --trace 运行它时,它一直说“找不到插件:[]”。有什么想法吗?
    • 非常感谢,解决方案是从 <a href="/default/index/tourl?u=aHR0cDovL2dpdGh1Yi5jb20vYW5kcmUvZ2Vva2l0LXJhaWxz" rel="nofollow" target="_blank">github.com/andre/geokit-rails</a> 下载代码的压缩版本(看来我的脚本/安装有一些问题)。运行 install.rb 文件完全解决了这个问题。再次感谢
    【解决方案2】:

    您使用什么方法来包含 geokit 库?插件?宝石?宝石是否已解压缩到供应商中?你的 environment.rb 是什么样的?

    编辑:我打算将此作为对问题的评论 - 请原谅我。

    【讨论】:

    • 我已将 gem 添加到项目中,但在我的项目文件夹中,供应商文件为空。我已经更新了我的帖子以添加有关相当空的 environment.rb 的详细信息。非常感谢您的帮助
    • 与我的一些项目相比,我看到的唯一不同寻常的是,您需要在模型文件中使用 activerecord 和 geokit - 这是不必要的,并且可能会导致并发症。删除这两行,然后重试。如果这不起作用,请尝试执行 rake gems:unpack to vendor your gems。
    • 谢谢,我已经完成了 rake gems:unpack,但是在模型文件中,如果我没有指定“require ....”,那么甚至无法识别 activerecord。我还在我的 environment.rb 中添加了一些代码(另一个编辑)。不幸的是,我仍然迷路了:s
    • 这个 Rails 应用程序有什么不标准的地方,或者你调用它的方式吗?你绝对不应该要求 activerecord - 我认为你有更大的问题。
    • 我不知道为什么,但是在迁移过程中创建我需要的东西,它利用 ActiveRecord,当我尝试 rake 文件时,它不会执行,除非我添加了 'require ..' =/ (我只是 ruby​​ on rails 的新手)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-07
    • 1970-01-01
    • 2011-10-25
    • 1970-01-01
    • 2022-07-13
    • 2016-02-23
    相关资源
    最近更新 更多