草庐IT

validation_methods

全部标签

ruby-on-rails - Rails - 测试 fixture 错误 NoMethodError : undefined method `type' for nil:NilClass

我在运行使用具有模型之间关联的固定装置的测试时遇到问题。这是我一运行raketest就得到的错误:ERROR["test_truth",SevenPortfolioTest,0.005154775]test_truth#SevenPortfolioTest(0.01s)NoMethodError:NoMethodError:undefinedmethod`type'fornil:NilClassERROR["test_should_destroy_item_video",SevenPortfolio::ItemVideosControllerTest,0.008887804]test_

ruby - alias_method 和 class_methods 不混合?

我一直在尝试修补全局缓存模块,但我不明白为什么它不起作用。有人有什么建议吗?这是错误:NameError:undefinedmethod`get'formodule`Cache'from(irb):21:in`alias_method'...由此代码生成:moduleCachedefself.getputs"original"endendmoduleCachedefself.get_modifiedputs"Newget"endenddefpeek_a_booCache.module_evaldo#make:get_not_modifiedalias_method:get_not_mo

ruby-on-rails - Rails 茧 gem : Undefined Method 'new_record?' on link_to_remove_association with Wicked

我一直在努力弄清楚一些代码。我有一个表格,我正在尝试使用Wicked和茧gem。一切正常,包括link_to_add_association功能。正如Cocoon所建议的那样,我正在为关联的表单字段呈现部分内容,并且除了link_to_remove_association函数之外,一切似乎都正常工作。它返回以下错误:nil:NilClass的未定义方法new_record?这是我抛出错误的部分:这是调用部分的View:location%>这是调用View的Controller操作:classUserStepsController如果有帮助,这里是cocoon中抛出错误的函数:defli

ruby-on-rails - Rails 的 ActiveRecord 序列化 :attr method gives "Missing Class or module error"

我试图在ActiveRecord模型中序列化一个简单的属性,而Rails2.3.4不喜欢它。classShopperserialize:tagsend>>a=Shopper.new=>>>a.tags=['aoeu','stnh']=>['aoeu','snth']>>a.save=>TypeError:classormodulerequired有人知道我错过了什么吗? 最佳答案 Arf...我以为我可以一次序列化两个属性,但事实并非如此:serialize:tags,:garments#thisiswrong第二个参数应该是序列化

ruby-on-rails - self.send(method, =, value) 不起作用

我正在尝试创建一个Ruby类,其中initialize方法接受选项的散列。然后,我将这些选项作为类的attr_accessor。现在,我可以做类似的事情classUserattr_accessor:name,:email,:phonedefinitialize(options)self.name=options[:name]self.email=options[:email]self.phone=options[:phone]endendUser.new(:name=>'SomeName',:email=>'some-name@some-company.com',:phone=>435

ruby-on-rails - class_methods 在关注中做了什么?

我正在阅读一些使用Rails4中关注点的代码。我看了一些文章说,如果我们想包含类方法使用模块ClassMethods,但我阅读的代码使用类似:class_methodsdodef****endend 最佳答案 ActiveSupport::Concern为模块混合的常见Ruby模式提供语法糖。当您使用modulesasmixins时你不能像在类中那样只使用self来声明类方法:moduleFoodefself.bar"HelloWorld"enddefinstance_method"HelloWorld"endendclassBaz

ruby-on-rails - rails : Validation in model vs migration

这个问题在这里已经有了答案:关闭10年前。PossibleDuplicate:RubyonRails:Isitbettertovalidateinthemodelorthedatabase?我看到可以在Rails模型和迁移中添加相同的约束/验证。但是哪一个是最好的方法呢?在模型和数据库级别进行验证是否是一种好的做法(以及为什么)?或者他们在rails上一样?例如我们可以在模型和迁移中对名称进行相同的验证classUsertrue,:presence=>trueendclassCreateUsertrue,:null=>falseendendend

ruby - 试图理解 self.method_name 与 Classname.method_name 在 Ruby 中的使用

我试图了解何时使用self.method_name与何时使用Classname.method_name。在下面的示例中,为什么“before_create”需要引用“User.hash_password”而不是“self.hash_password”或只是“hash_password”?由于我们已经在User类中,我认为before_create方法会“知道”“hash_password”是它自己的类的成员,不需要任何特殊语法来引用它。require'digest/sha1'classUser["name=?andhashed_password=?",name,hashed_passw

ruby-on-rails - rails 3 : validate presence of at least one has many through association item

我有两个模型:Project和ProjectDiscipline:classProject:destroyhas_many:project_disciplines,through::project_disciplinizationsattr_accessible:project_discipline_idsattr_accessible:project_disciplines_attributesaccepts_nested_attributes_for:project_disciplines,:reject_if=>proc{|attributes|attributes['name'

ruby-on-rails - rails : validating a field is present only if another is present

我有一个模型,其中有两个字段在技术上可以为空。字段名称是:is_activated和:activated_at。:activated_at仅在:is_activated设置为true时才需要。如果:is_activated为false,则不需要存在。在Rails中将此验证直接设置到ActiveRecord中的合适方法是什么? 最佳答案 您可以使用Proc在:activated_at验证器上。validates:activated_at,:presence,if:Proc.new{|a|a.is_activated?}推荐阅读:htt