rails g mongoid:配置不工作 Rails 6.1
Posted
技术标签:
【中文标题】rails g mongoid:配置不工作 Rails 6.1【英文标题】:rails g mongoid:config not working Rails 6.1 【发布时间】:2021-04-07 21:46:28 【问题描述】:我在 Mac Sierra 上使用 Rails 6.1,并尝试安装 Mongodb 作为数据库, 已经在 bash 和 gem 上安装了 mongo db 服务,但是当我输入:
bin/rails g mongoid:config
它显示以下错误:
Traceback (most recent call last):
26: from bin/rails:5:in `<main>'
25: from /Users/guilherme/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/activesupport-6.1.0/lib/active_support/dependencies.rb:332:in `require'
24: from /Users/guilherme/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/activesupport-6.1.0/lib/active_support/dependencies.rb:299:in `load_dependency'
23: from /Users/guilherme/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/activesupport-6.1.0/lib/active_support/dependencies.rb:332:in `block in require'
22: from /Users/guilherme/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/bootsnap-1.5.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:31:in `require'
21: from /Users/guilherme/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/bootsnap-1.5.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `require_with_bootsnap_lfi'
20: from /Users/guilherme/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/bootsnap-1.5.1/lib/bootsnap/load_path_cache/loaded_features_index.rb:92:in `register'
19: from /Users/guilherme/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/bootsnap-1.5.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `block in require_with_bootsnap_lfi'
18: from /Users/guilherme/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/bootsnap-1.5.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `require'
17: from /Users/guilherme/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/railties-6.1.0/lib/rails/commands.rb:18:in `<main>'
16: from /Users/guilherme/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/railties-6.1.0/lib/rails/command.rb:50:in `invoke'
15: from /Users/guilherme/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/railties-6.1.0/lib/rails/command/base.rb:69:in `perform'
14: from /Users/guilherme/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/thor-1.0.1/lib/thor.rb:392:in `dispatch'
13: from /Users/guilherme/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/thor-1.0.1/lib/thor/invocation.rb:127:in `invoke_command'
12: from /Users/guilherme/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/thor-1.0.1/lib/thor/command.rb:27:in `run'
11: from /Users/guilherme/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/railties-6.1.0/lib/rails/commands/generate/generate_command.rb:26:in `perform'
10: from /Users/guilherme/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/railties-6.1.0/lib/rails/generators.rb:275:in `invoke'
9: from /Users/guilherme/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/thor-1.0.1/lib/thor/base.rb:485:in `start'
8: from /Users/guilherme/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/thor-1.0.1/lib/thor/group.rb:232:in `dispatch'
7: from /Users/guilherme/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/thor-1.0.1/lib/thor/invocation.rb:134:in `invoke_all'
6: from /Users/guilherme/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/thor-1.0.1/lib/thor/invocation.rb:134:in `map'
5: from /Users/guilherme/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/thor-1.0.1/lib/thor/invocation.rb:134:in `each'
4: from /Users/guilherme/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/thor-1.0.1/lib/thor/invocation.rb:134:in `block in invoke_all'
3: from /Users/guilherme/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/thor-1.0.1/lib/thor/invocation.rb:127:in `invoke_command'
2: from /Users/guilherme/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/thor-1.0.1/lib/thor/command.rb:27:in `run'
1: from /Users/guilherme/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/bundler/gems/mongoid-3c6bdaced7d8/lib/rails/generators/mongoid/config/config_generator.rb:18:in `app_name'
/Users/guilherme/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/railties-6.1.0/lib/rails/railtie.rb:209:in method_missing': undefined method
parent ' 对于 Code7::Application:Class (NoMethodError)
你的意思是?在场吗?
试图停止 Spring 服务器,没有成功。
这是gemfile
source 'https://rubygems.org'
git_source(:github) |repo| "https://github.com/#repo.git"
ruby '2.7.2'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 6.1.0'
# Use Puma as the app server
gem 'puma', '~> 5.0'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
# gem 'jbuilder', '~> 2.7'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 4.0'
# Use Active Model has_secure_password
# gem 'bcrypt', '~> 3.1.7'
# MongoDB
gem 'mongoid', git: 'https://github.com/mongodb/mongoid.git', branch: 'master'
# Reduces boot times through caching; required in config/boot.rb
gem 'bootsnap', '>= 1.4.4', require: false
# Use Rack CORS for handling Cross-Origin Resource Sharing (CORS), making cross-origin AJAX possible
# gem 'rack-cors'
group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
end
group :development do
gem 'listen', '~> 3.3'
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
# gem 'spring'
end
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
提前致谢!
【问题讨论】:
看起来像另一个 6.1 兼容性问题。 【参考方案1】:只是一个解决方法
-
打开config_generator.rb(根据你的机器改路径)
nano /usr/local/bundle/bundler/gems/mongoid-d673c43e8846/lib/rails/generators/mongoid/config/config_generator.rb`
-
替换
def app_name Rails::Application.subclasses.first.parent.to_s.underscore end
为
def app_name Rails::Application.subclasses.first.to_s.split('::').first end
或
def app_name 'your_app_name' end
不确定以后会不会有副作用,但至少我可以运行它
【讨论】:
【参考方案2】:您可以通过放置如下文件来解决此错误:
config/initializers/fix_mongoid_generator.rb
require 'rails/generators'
require 'rails/generators/mongoid/config/config_generator'
if Gem::Version.new(Mongoid::VERSION) >= Gem::Version.new('7.3.0')
warn("You may need not this file: #__FILE__\nAfter https://github.com/mongodb/mongoid/pull/4953 is released.")
end
Mongoid::Generators::ConfigGenerator.class_eval do
def app_name
# Rails::Application.subclasses.first.parent.to_s.underscore
# No more Module#parent
# See https://github.com/rails/rails/commit/167b4153cac0069a21e0bb9689cb16f34f6abbaa
Rails::Application.subclasses.first.module_parent_name.underscore
end
end
我希望这个错误很快就会得到修复。
https://github.com/mongodb/mongoid/pull/4953
【讨论】:
以上是关于rails g mongoid:配置不工作 Rails 6.1的主要内容,如果未能解决你的问题,请参考以下文章
Heroku 的 Rails、Mongoid 和 Unicorn 配置
在非 Rails 环境中通过 Mongoid 创建 MongoDB 索引