我在哪里将config / application.rb中的自定义验证程序的自动加载路径放在哪里 [英] Where exactly do I put autoload path for custom validator in config/application.rb

查看:134
本文介绍了我在哪里将config / application.rb中的自定义验证程序的自动加载路径放在哪里的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每次我用NumberValidator调用validates_with。我收到一个未初始化的常量错误。我在 app目录下创建了一个名为验证器的目录。

Every time I call validates_with NumberValidator. I get an uninitialized constant error. I have created a directory called validators under the "app" directory. This is what I have so far.

这是我的模型

class Worker < ActiveRecord::Base
    include ActiveModel::Validations
validates_with NumberValidator

end

这是我的验证器文件

class NumberValidator < ActiveModel::Validator
    def validate(record, attribute, value)
        puts record
        puts attribute
        puts value
    end
end

require File.expand_path('../boot', __FILE__)

require "rails"
# Pick the frameworks you want:
require "active_model/railtie"
require "active_job/railtie"
require "active_record/railtie"
require "action_controller/railtie"
require "action_mailer/railtie"
require "action_view/railtie"
require "sprockets/railtie"
# 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 ApplicationName
  class Application < Rails::Application
    # Settings in config/environments/* take precedence over those specified here.
    # Application configuration should go into files in config/initializers
    # -- all .rb files in that directory are automatically loaded.

    # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
    # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
    # config.time_zone = 'Central Time (US & Canada)'

    # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
    # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
    # config.i18n.default_locale = :de

    # Do not swallow errors in after_commit/after_rollback callbacks.
    config.active_record.raise_in_transactional_callbacks = true

    config.autoload_paths += %W["#{config.root}/app/validators/"]
  end
end

我已经重新启动服务器,但是我不确定自己在做什么错

I have restarted my server and I'm just not sure what I am doing wrong

推荐答案

如果遵循预期的结构,Rails将自动加载 app / 下的任何内容。 Rails的方法是使用常量自动加载,该常量将常量(例如 MyValidator )映射到文件名(例如 my_validator.rb 在自动加载路径中的目录下。

Rails will automatically load anything under app/ assuming you follow the expected structure. The way Rails does this is with Constant Autoloading, which maps a constant, such as MyValidator to a file name, such as my_validator.rb under a directory in the autoload paths.

为了让Rails加载 NumberValidator ,您需要应该将文件命名为 number_validator.rb 并将其放在自动加载路径的文件夹中,例如 app / validators / number_validator.rb 。如果您在 app 下添加了目录,则需要重新启动服务器,因为这些目录是在引导时初始化的(请确保运行 spring stop (如果您使用的是spring,因此也会重新启动!)。

In order for Rails to load your NumberValidator, you should name the file number_validator.rb and put it in a folder in the autoload path, such as app/validators/number_validator.rb. You will need to restart the server if you have added a directory under app because these are initialized at boot time (be sure to run spring stop if you are using spring so it restarts too!).

注意:


  1. 不需要需要将 app 下的任何内容添加到您的应用配置中,因此您可以删除 config.autoload_paths 行。

  2. ActiveRecord :: Base 已包含 ActiveModel :: Validations ,因此您也可以从您的 Worker 类中删除该 include 。 / li>
  1. You do not need to add anything under app to your app config, so you can remove the config.autoload_paths line.
  2. ActiveRecord::Base already includes ActiveModel::Validations, so you can also remove that include from your Worker class.

有关此过程如何工作的更多信息,请查看 Rails指南中的自动加载和重新加载常量页面。

For more information on how this process works, check out the Autoloading and Reloading Constants page in the Rails Guides.

这篇关于我在哪里将config / application.rb中的自定义验证程序的自动加载路径放在哪里的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆