Rails为什么找不到宝石?

时间:2019-03-09 21:07:45

标签: ruby-on-rails ruby rails-geocoder

我在我的gemfile中安装了地址解析器gem:

gem 'geocoder', '~> 1.5.1'

然后我将此行添加到模型中

class Place < ApplicationRecord 
  geocoded_by :address
end

我收到此错误:

undefined method `geocoded_by' for #<Class:0x00007f8b58b763b8>

知道为什么会出现此错误吗?我认为安装gem就足够了,但是显然,Rails无法找到Geocoder库。

更新#1 这是我的gemfile

source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }

ruby '2.5.1'
gem 'rails', '~> 5.2.2'
gem 'pg', '>= 0.18', '< 2.0'
gem 'puma', '~> 3.11'
gem 'sass-rails', '~> 5.0'
gem 'uglifier', '>= 1.3.0'
gem 'mini_racer', platforms: :ruby
gem 'coffee-rails', '~> 4.2'
gem 'turbolinks', '~> 5'
gem 'jbuilder', '~> 2.5'
gem 'bootsnap', '>= 1.1.0', require: false
gem 'impressionist', '~> 1.5', '>= 1.5.1'
gem 'geocoder', '~> 1.5', '>= 1.5.1'

group :development, :test do
  gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
end

group :development do
  gem 'web-console', '>= 3.3.0'
  gem 'listen', '>= 3.0.5', '< 3.2'
  gem 'spring'
  gem 'spring-watcher-listen', '~> 2.0.0'
end

group :test do
  gem 'capybara', '>= 2.15'
  gem 'rspec-rails'
  gem 'selenium-webdriver'
  gem 'chromedriver-helper'
end

gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

更新#2 当我运行gem list geocoder时,输出如下:

*** LOCAL GEMS ***

geocoder (default: 1.5.1, 1.4.8, 1.4.7)

更新#3 我不认为这只是地理编码器...

我将此添加到了我的gemfile中:

group :development do
  # other gems are here, they're just not listed.
  gem "better_errors"
  gem "binding_of_caller"
end

但是,我仍然遇到常规错误。所以现在,甚至没有发现更好的宝石错误!!!

更新#4 添加我的位置模型和应用程序记录

class Place < ApplicationRecord
  extend FriendlyId
  friendly_id :name, use: :slugged
  geocoded_by :address
end

class ApplicationRecord < ActiveRecord::Base
  self.abstract_class = true
end

更新#5 Github发布建议

建议将其添加到模型文件的顶部:

extend Geocoder::Model::ActiveRecord

这是一种变通解决方案,但甚至没有用!

我收到此错误:uninitialized constant Place::Geocoder

更新#6 |临时修复

我将gem 'geocoder', '~> 1.5.1'降级为gem 'geocoder', '1.5'

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

我认为问题是必须要做的 bundle update。为此,始终必须在Gemfile中添加gem。