Ruby on rails 为什么我的应用程序/验证程序会出错?
我一直在尝试验证名字是否只包含字母、数字、连字符或下划线 在my users.rb模型中,我使用以下代码:Ruby on rails 为什么我的应用程序/验证程序会出错?,ruby-on-rails,ruby,ruby-on-rails-3,validation,customvalidator,Ruby On Rails,Ruby,Ruby On Rails 3,Validation,Customvalidator,我一直在尝试验证名字是否只包含字母、数字、连字符或下划线 在my users.rb模型中,我使用以下代码: validates :first_namename, :firstname_convention => true 这将转到FirstNameConvention类: class FirstnameConventionValidator < ActiveModel::EachValidator def validate_each(record, field, value)
validates :first_namename, :firstname_convention => true
这将转到FirstNameConvention类:
class FirstnameConventionValidator < ActiveModel::EachValidator
def validate_each(record, field, value)
unless value.blank?
record.errors[field] << "is not alphanumeric (letters, numbers, underscores or periods)" unless value =~ /^[[:alnum:]._-]+$/
record.errors[field] << "should start with a letter" unless value[0] =~ /[A-Za-z]/
record.errors[field] << "contains illegal characters" unless value.ascii_only?
end
end
end
我尝试过:
config.autoload_paths += %W["#{config.root}/app/validators"]
在config/application.rb中:
require File.expand_path('../boot', __FILE__)
require 'rails/all'
# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)
module ThorCinema
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 << Rails.root.join('app', 'validators')
end
end
需要文件。展开路径('../boot',文件)
需要“rails/all”
#需要Gemfile中列出的宝石,包括任何宝石
#您仅限于:测试、开发或:生产。
Bundler.require(*Rails.groups)
电影模块
类应用程序 config.autoload_pathapp/validators
是存储验证器的好地方,您所需要做的就是告诉Rails这个文件夹的存在。将此行添加到config/application.rb
(YourAppName::application
部分)中的自动加载路径:
config.autoload_路径应用程序/验证程序可能不在通常的Rails路径中。试着把你的文件夹放到lib/validators中,看看这是否对你有效,所以把validators文件夹放在那里?是的。这是我的建议我应该在哪里创建lib文件夹?在默认站点目录中?lib文件夹应该已经存在(在应用程序的根目录中)!Rails将查看这个文件夹,如果它找不到类等。我已经更新了这个问题,以显示我把它放在了哪里,但它仍然出现了相同的错误。是因为我在应用程序中创建了validators文件夹,而不是rails生成的吗?@benjs.1请确保重新启动了服务器/控制台。不,不需要使用发电机。您可以根据需要扩展应用程序。
require File.expand_path('../boot', __FILE__)
require 'rails/all'
# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)
module ThorCinema
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 << Rails.root.join('app', 'validators')
end
end
config.autoload_paths << Rails.root.join('app', 'validators')