我花了几个小时试图找到问题的解决方案,但却没有理解我做错了什么。
Rails 4.2.6
我的模特:
app/models/component.rb
class Component < ActiveRecord::Base
validates :name, presence: true,
uniqueness: true,
forbid_changing: true #TODO [VS] Fix custom validator autoload
validates :label, presence: true,
uniqueness: true
end
我的验证员:
class ForbidChangingValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
if record.send "#{attribute}_changed?".to_sym
record.errors[attribute] << options[:message] || t(:changing_forbidden)
end
end
end
当尝试重新加载页面时出现错误:
Unknown validator: 'ForbidChangingValidator'
ArgumentError - Unknown validator: 'ForbidChangingValidator':
activemodel (4.2.6) lib/active_model/validations/validates.rb:120:in `rescue in block in validates'
activemodel (4.2.6) lib/active_model/validations/validates.rb:117:in `block in validates'
activemodel (4.2.6) lib/active_model/validations/validates.rb:113:in `validates'
app/models/component.rb:6:in `<class:Component>'
app/models/component.rb:1:in `<top (required)>'
activesupport (4.2.6) lib/active_support/dependencies.rb:457:in `block in load_file'
activesupport (4.2.6) lib/active_support/dependencies.rb:647:in `new_constants_in'
activesupport (4.2.6) lib/active_support/dependencies.rb:456:in `load_file'
activesupport (4.2.6) lib/active_support/dependencies.rb:354:in `require_or_load'
activesupport (4.2.6) lib/active_support/dependencies.rb:494:in `load_missing_constant'
activesupport (4.2.6) lib/active_support/dependencies.rb:184:in `const_missing'
activesupport (4.2.6) lib/active_support/inflector/methods.rb:261:in `block in constantize'
activesupport (4.2.6) lib/active_support/inflector/methods.rb:259:in `constantize'
activesupport (4.2.6) lib/active_support/core_ext/string/inflections.rb:66:in `constantize'
repositor (0.6.0) lib/repositor/active_record.rb:6:in `initialize'
但是当我试图从控制台调用此验证器时,我得到了:
!! #<ArgumentError: A copy of Component has been removed from the module tree but is still active!>
我发现了很多关于这个问题的消息,但是无法解决我的问题。
请帮助别人......
答案 0 :(得分:2)
我发现了这个问题。这是关于命名政策。 rails尝试使用验证器名称的方式很奇怪。
我将验证器重命名为ForbidChangesValidator
并将其归档。
一切都很顺利。
请注意。
答案 1 :(得分:0)
使用ActiveModel :: EachValidator进行的自定义验证需要在根名称空间或“核心”自动加载路径(无论您如何定义这些目录)中声明。。
例如: (“是”表示可以使用,而“否”则无法使用)
WHERE
=> 是,因为它是app /的新目录,因此可以使用根名称空间
SELECT p.*
FROM products p
WHERE p.customer_id = 24
OR (
p.customer_id = 0
AND
NOT EXISTS (SELECT 1 FROM products WHERE product_id = p.product_id AND customer_id = 24)
)
=> 是,因为它是核心目录
> id | product_id | active | customer_id
> -: | ---------: | -----: | ----------:
> 2 | 100 | 0 | 24
> 4 | 101 | 0 | 24
> 5 | 103 | 1 | 0
=> 是,因为它是核心目录
app/validations
=> 否,因为它是核心目录下的子目录,因此需要名称间距。
答案 2 :(得分:0)
重新启动服务器为我解决了这个问题,因为其他一切都正确
答案 3 :(得分:-1)
确保您在NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDate *currentDate = [NSDate date];
NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setYear:30];
NSDate *maxDate = [calendar dateByAddingComponents:comps toDate:currentDate options:0];
[comps setYear:-30];
NSDate *minDate = [calendar dateByAddingComponents:comps toDate:currentDate options:0];
[datePicker setMaximumDate:maxDate];
[datePicker setMinimumDate:minDate];
定义ForbidChangingValidator
并将配置添加到:
<强>配置/ application.rb中强>
app/validators/forbid_changing_validator.rb