rails g mongoid:config 不工作 Rails 6.1

rails g mongoid:config not working Rails 6.1

我在 Mac Sierra 上使用 Rails 6.1,并尝试将 Mongodb 安装为数据库, 已经在 bash 和 gem 上安装了 mongo 数据库服务,但是当我输入:

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' for Code7::Application:Class (NoMethodError) 你的意思?现在?

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

这是gem文件

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]

提前致谢!

只是一个解决方法

  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