RAILS_ENV=development rails c

是工作,

RAILS_ENV=test rails c

收到此错误

# RAILS_ENV=test rails c
/home/seluser/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/railties-7.0.8/lib/rails.rb:47:in `configuration': undefined method `config' for nil:NilClass (NoMethodError)

      application.config
                 ^^^^^^^
        from /home/seluser/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/factory_bot_rails-6.4.0/lib/factory_bot_rails/railtie.rb:25:in `block in <class:Railtie>'
        from /home/seluser/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/activesupport-7.0.8/lib/active_support/lazy_load_hooks.rb:95:in `class_eval'
        from /home/seluser/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/activesupport-7.0.8/lib/active_support/lazy_load_hooks.rb:95:in `block in execute_hook'
        from /home/seluser/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/activesupport-7.0.8/lib/active_support/lazy_load_hooks.rb:85:in `with_execution_control'
        from /home/seluser/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/activesupport-7.0.8/lib/active_support/lazy_load_hooks.rb:90:in `execute_hook'
        from /home/seluser/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/activesupport-7.0.8/lib/active_support/lazy_load_hooks.rb:60:in `block in on_load'
        from /home/seluser/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/activesupport-7.0.8/lib/active_support/lazy_load_hooks.rb:59:in `each'
        from /home/seluser/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/activesupport-7.0.8/lib/active_support/lazy_load_hooks.rb:59:in `on_load'
        from /home/seluser/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/factory_bot_rails-6.4.0/lib/factory_bot_rails/railtie.rb:24:in `<class:Railtie>'
        from /home/seluser/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/factory_bot_rails-6.4.0/lib/factory_bot_rails/railtie.rb:10:in `<module:FactoryBotRails>'
        from /home/seluser/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/factory_bot_rails-6.4.0/lib/factory_bot_rails/railtie.rb:9:in `<main>'
        from <internal:/home/seluser/.rbenv/versions/3.2.2/lib/ruby/3.2.0/rubygems/core_ext/kernel_require.rb>:37:in `require'
        from <internal:/home/seluser/.rbenv/versions/3.2.2/lib/ruby/3.2.0/rubygems/core_ext/kernel_require.rb>:37:in `require'
        from /home/seluser/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/bootsnap-1.17.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:32:in `require'
        from /home/seluser/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/factory_bot_rails-6.4.0/lib/factory_bot_rails.rb:1:in `<main>'
        from <internal:/home/seluser/.rbenv/versions/3.2.2/lib/ruby/3.2.0/rubygems/core_ext/kernel_require.rb>:37:in `require'
        from <internal:/home/seluser/.rbenv/versions/3.2.2/lib/ruby/3.2.0/rubygems/core_ext/kernel_require.rb>:37:in `require'
        from /home/seluser/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/bootsnap-1.17.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:32:in `require'
        from /home/seluser/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/bundler-2.4.18/lib/bundler/runtime.rb:60:in `block (2 levels) in require'
        from /home/seluser/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/bundler-2.4.18/lib/bundler/runtime.rb:55:in `each'
        from /home/seluser/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/bundler-2.4.18/lib/bundler/runtime.rb:55:in `block in require'
        from /home/seluser/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/bundler-2.4.18/lib/bundler/runtime.rb:44:in `each'
        from /home/seluser/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/bundler-2.4.18/lib/bundler/runtime.rb:44:in `require'
        from /home/seluser/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/bundler-2.4.18/lib/bundler.rb:187:in `require'
        from /home/seluser/app/配置/应用程序.rb:19:in `<main>'
        from <internal:/home/seluser/.rbenv/versions/3.2.2/lib/ruby/3.2.0/rubygems/core_ext/kernel_require.rb>:37:in `require'
        from <internal:/home/seluser/.rbenv/versions/3.2.2/lib/ruby/3.2.0/rubygems/core_ext/kernel_require.rb>:37:in `require'
        from /home/seluser/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/bootsnap-1.17.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:32:in `require'
        from /home/seluser/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/railties-7.0.8/lib/rails/command/actions.rb:22:in `require_application!'
        from /home/seluser/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/railties-7.0.8/lib/rails/command/actions.rb:14:in `require_application_and_environment!'
        from /home/seluser/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/railties-7.0.8/lib/rails/commands/console/console_command.rb:105:in `perform'
        from /home/seluser/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/thor-1.3.0/lib/thor/command.rb:28:in `run'
        from /home/seluser/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/thor-1.3.0/lib/thor/invocation.rb:127:in `invoke_command'
        from /home/seluser/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/thor-1.3.0/lib/thor.rb:527:in `dispatch'
        from /home/seluser/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/railties-7.0.8/lib/rails/command/base.rb:87:in `perform'
        from /home/seluser/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/railties-7.0.8/lib/rails/command.rb:48:in `invoke'
        from /home/seluser/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/railties-7.0.8/lib/rails/commands.rb:18:in `<main>'
        from <internal:/home/seluser/.rbenv/versions/3.2.2/lib/ruby/3.2.0/rubygems/core_ext/kernel_require.rb>:37:in `require'
        from <internal:/home/seluser/.rbenv/versions/3.2.2/lib/ruby/3.2.0/rubygems/core_ext/kernel_require.rb>:37:in `require'
        from /home/seluser/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/bootsnap-1.17.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:32:in `require'
        from bin/rails:4:in `<main>'

请帮帮我.

Environment

rails 7.0.8
ruby 3.2.2

config/environments/development.rb

require "active_support/core_ext/integer/time"

Rails.application.configure do
  # Settings specified here will take precedence over those in 配置/应用程序.rb.

  # In the development environment your application's code is reloaded any time
  # it changes. This slows down response time 但 is perfect for development
  # since you don't have to restart the web server when you make code changes.
  config.cache_classes = false

  # Do not eager load code on boot.
  config.eager_load = false

  # Show full error reports.
  config.consider_all_requests_local = true

  # Enable server timing
  config.server_timing = true

  # Enable/disable caching. By default caching is disabled.
  # Run rails dev:cache to toggle caching.
  if Rails.root.join("tmp/caching-dev.txt").exist?
    config.action_controller.perform_caching = true
    config.action_controller.enable_fragment_cache_logging = true

    config.cache_store = :memory_store
    config.public_file_server.headers = {
      "Cache-Control" => "public, max-age=#{2.days.to_i}"
    }
  else
    config.action_controller.perform_caching = false

    config.cache_store = :null_store
  end

  # Store uploaded files on the local file system (see config/storage.yml for options).
  config.active_storage.service = :local

  # Don't care if the mailer can't send.
  config.action_mailer.raise_delivery_errors = false

  config.action_mailer.perform_caching = false

  config.action_mailer.delivery_method = :letter_opener_web

  # Print deprecation notices to the Rails logger.
  config.active_support.deprecation = :log

  # Raise exceptions for disallowed deprecations.
  config.active_support.disallowed_deprecation = :raise

  # Tell Active Support which deprecation messages to disallow.
  config.active_support.disallowed_deprecation_warnings = []

  # Raise an error on page load if there are pending migrations.
  config.active_record.migration_error = :page_load

  # Highlight code that triggered database queries in logs.
  config.active_record.verbose_query_logs = true

  # Suppress logger output for asset requests.
  config.assets.quiet = true

  # Raises error for missing translations.
  # config.i18n.raise_on_missing_translations = true

  # Annotate rendered view with file names.
  # config.action_view.annotate_rendered_view_with_filenames = true

  # Uncomment if you wish to allow Action Cable access from any origin.
  # config.action_cable.disable_request_forgery_protection = true

  LetterOpenerWeb.configure do |config|
    config.letters_location = Rails.root.join('log/letter_opener')
  end
end

配置/环境/est.rb

require "active_support/core_ext/integer/time"

# The test environment is used exclusively to run your application's
# test suite. You never need to work with it otherwise. Remember that
# your test database is "scratch space" for the test suite and is wiped
# and recreated between test runs. Don't rely on the data there!

Rails.application.configure do
  # Settings specified here will take precedence over those in 配置/应用程序.rb.

  # Turn false under Spring and add config.action_view.cache_template_loading = true.
  config.cache_classes = true

  # Eager loading loads your whole application. When running a single test locally,
  # this probably isn't necessary. It's a good idea to do in a continuous integration
  # system, or in some way before deploying your code.
  config.eager_load = ENV["CI"].present?

  # Configure public file server for tests with Cache-Control for performance.
  config.public_file_server.enabled = true
  config.public_file_server.headers = {
    "Cache-Control" => "public, max-age=#{1.hour.to_i}"
  }

  # Show full error reports and disable caching.
  config.consider_all_requests_local       = true
  config.action_controller.perform_caching = false
  config.cache_store = :null_store

  # Raise exceptions instead of rendering exception templates.
  config.action_dispatch.show_exceptions = false

  # Disable request forgery protection in test environment.
  config.action_controller.allow_forgery_protection = false

  # Store uploaded files on the local file system in a temporary directory.
  config.active_storage.service = :test

  config.action_mailer.perform_caching = false

  # Tell Action Mailer not to deliver emails to the real world.
  # The :test delivery method accumulates sent emails in the
  # ActionMailer::Base.deliveries array.
  config.action_mailer.delivery_method = :test

  # Print deprecation notices to the stderr.
  config.active_support.deprecation = :stderr

  # Raise exceptions for disallowed deprecations.
  config.active_support.disallowed_deprecation = :raise

  # Tell Active Support which deprecation messages to disallow.
  config.active_support.disallowed_deprecation_warnings = []

  # Raises error for missing translations.
  # config.i18n.raise_on_missing_translations = true

  # Annotate rendered view with file names.
  # config.action_view.annotate_rendered_view_with_filenames = true
end

配置/应用程序.rb

require_relative 'boot'

require "rails"
# Pick the frameworks you want:
require "active_model/railtie"
require "active_job/railtie"
require "active_record/railtie"
require "active_storage/engine"
require "action_controller/railtie"
require "action_mailer/railtie"
require "action_mailbox/engine"
require "action_text/engine"
require "action_view/railtie"
# require "action_cable/engine"
require "rails/test_unit/railtie"

# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)

module App
  class Application < Rails::Application
    # Initialize configuration defaults for originally generated Rails version.
    config.load_defaults 7.0

    # Don't generate system test files.
    config.generators.system_tests = nil

    # config.i18n.default_locale = :ja

    config.time_zone = 'Asia/Tokyo'

    # 本番環境でもauto_loadを有効にする
    config.enable_dependency_loading = true

    # デザイン崩れるので、自動で読み込まれるfield_with_errorsタグを読み込まないように設定する.まさにこの記事.@see https://qiita.com/uminchu-1173/items/bb1ffab51aa9eb9d98fd
    config.action_view.field_error_proc = Proc.new { |html_tag, instance| html_tag }


    config.autoload_paths += %W(#{config.root}/lib)
    config.autoload_paths += %W(#{Rails.root}/app/search_form)
    config.autoload_paths += %W(#{Rails.root}/app/batches)
    config.autoload_paths += %W(#{Rails.root}/app/services)
    config.autoload_paths += %W(#{Rails.root}/app/errors)

    config.autoload_paths += %W(#{Rails.root}/errors)
  end
end

By the way..

小费1

我在下面也试过了.

cp config/environments/development.rb 配置/环境/est.rb
RAILS_ENV=test rails c

但 error was no change.

小费2

# RAILS_ENV=test rails db:migrate
rails aborted!
NoMethodError: undefined method `config' for nil:NilClass (NoMethodError)

      application.config
                 ^^^^^^^
<internal:/home/seluser/.rbenv/versions/3.2.2/lib/ruby/3.2.0/rubygems/core_ext/kernel_require.rb>:37:in `require'
<internal:/home/seluser/.rbenv/versions/3.2.2/lib/ruby/3.2.0/rubygems/core_ext/kernel_require.rb>:37:in `require'
<internal:/home/seluser/.rbenv/versions/3.2.2/lib/ruby/3.2.0/rubygems/core_ext/kernel_require.rb>:37:in `require'
<internal:/home/seluser/.rbenv/versions/3.2.2/lib/ruby/3.2.0/rubygems/core_ext/kernel_require.rb>:37:in `require'
/home/seluser/app/配置/应用程序.rb:19:in `<main>'
/home/seluser/app/rakefile:4:in `require_relative'
/home/seluser/app/rakefile:4:in `<main>'
<internal:/home/seluser/.rbenv/versions/3.2.2/lib/ruby/3.2.0/rubygems/core_ext/kernel_require.rb>:37:in `require'
<internal:/home/seluser/.rbenv/versions/3.2.2/lib/ruby/3.2.0/rubygems/core_ext/kernel_require.rb>:37:in `require'
bin/rails:4:in `<main>'

推荐答案

你需要把factory_bot_rails升级到v6.4.2

100


Also note that directories under app/ are automatically added to autoload paths:
ActiveSupport::Dependencies.autoload_paths

So you don't need to do this:
config.autoload_paths += %W(#{Rails.root}/app/search_form).

Ruby-on-rails相关问答推荐

在数组中定位不匹配的索引

在单个继承表和另一个表中的类之间建立多对多关联

URL 中的 Rails slugs - 使用 Active Record Model Post 的 Title 属性而不是 ID

Rails 路由到唯一索引

rake assets:precompile try 连接到数据库

Rails:保存子对象时更新父对象

有没有办法在模型而不是视图中使用复数()?

带有 Cucumber 和 rspec 的 BDD - 这什么时候是多余的?

如何在 Rails 中使用 instance_eval 子句删除验证?

如何使用 capistrano deploy 定位特定的提交 SHA

将 Amazon SES 与 Rails ActionMailer 结合使用

如何在 RoR 中实现成就系统

如何在不删除项目本身的情况下删除单个 HABTM 关联项目?

Rails:使用 gem 卸载特定版本的库

导轨链接到:远程

Django 还是 Ruby-On-Rails?

Rails 4模块的未初始化常量

Rails 4,Capistrano 3.0.0,无法加载这样的文件 - 部署

rake 任务中的 def 块

Rails:如何将日期时间字符串解析为特定时区