rails g mongoid:config 不工作 Rails 6.1

问题描述

我在 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 {{1} Code7::Application:Class 的 }parent' (NoMethodError) 你的意思?存在吗?

试图停止 Spring 服务器,没有成功。

这里是宝石文件

method_missing': undefined method 

提前致谢!

解决方法

只是一个解决方法

  1. 打开config_generator.rb(根据你的机器修改路径)

nano /usr/local/bundle/bundler/gems/mongoid-d673c43e8846/lib/rails/generators/mongoid/config/config_generator.rb`

  1. 替换
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

不确定这以后会不会有副作用,但至少我可以运行它

,

您可以通过放置如下文件来解决此错误:

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