简体   繁体   中英

Why can't Rails locate gems?

I installed the geocoder gem in my gemfile like this:

gem 'geocoder', '~> 1.5.1'

Then I added this line to my model

class Place < ApplicationRecord 
  geocoded_by :address
end

I get this error:

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

Any idea why I'm getting this error? I figured installing the gem would be enough, but apparently, Rails can't locate the Geocoder library.

UPDATE #1 Here is my 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]

UPDATE #2 When I run gem list geocoder , This is what outputs:

*** LOCAL GEMS ***

geocoder (default: 1.5.1, 1.4.8, 1.4.7)

UPDATE #3 I don't think it's just geocoder...

I added this to my gemfile:

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

However, I'm still getting regular errors. So now, not even the better gems error is being found!!!

UPDATE #4 Adding my Place Model and application record

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

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

UPDATE #5 Github issues recommendation

It was recommended to add this at the top of my model file:

extend Geocoder::Model::ActiveRecord

This was sort of a work-around solution, but not even this worked!

I get this error: uninitialized constant Place::Geocoder

UPDATE #6 | Temporary Fix

I downgraded gem 'geocoder', '~> 1.5.1' to gem 'geocoder', '1.5'

Any ideas?

我认为问题在于必须进行bundle update始终必须在Gemfile中添加gem。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM