【发布时间】:2014-04-10 19:55:21
【问题描述】:
我正在为名为 Project 的模型中的属性编写自定义验证方法。这是我的代码:
def self.skill_options
categories = Craft.all.collect{|craft| craft.label} #should change this later
return categories
end
validate :validate_tag_list
def validate_tag_list
puts skill_options.inspect.to_s
puts 'validate tag list is happening'
self.tag_list.each do |tag|
if self.skill_options.include?(tag)
puts tag.to_s + 'is included yo'
else
puts tag.to_s + 'not included yo'
self.errors.add(:tag_list, "#{tag} is not a valid skill.")
end
end
end
由于某种原因,有人告诉我:
NameError (undefined local variable or method `skill_options' for #<Project:0x007fb6fc02ac28>)
我不确定这是为什么。我在同一模型中对名为 category 的不同属性进行了另一个验证。此验证非常有效。代码如下:
def self.category_options
categories = Craft.all.collect{|craft| craft.label} #should change this later
end
validates :category, inclusion: {in: category_options}
唯一的区别是第一次验证(关于技能)需要自定义验证,因为它是一个数组。
如何消除错误?
【问题讨论】:
标签: ruby-on-rails ruby validation model-view-controller ruby-on-rails-4