Tag: validation

Rails 3.2唯一性validation引发未定义的方法’零?’ 为零:Nilclass

我正在使用Rails 3.2.0。 我有一个简单的模型,如下所示 class Favorite true validates :uuid, :presence => true, :uniqueness => {:scope => :lst} end 如果我试试这个 f = Favorite.new f.valid? 我收到以下错误消息: NoMethodError: undefined method `zero?’ for nil:NilClass from /Users/ragrawal/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.0/lib/active_record/associations/alias_tracker.rb:28:in `aliased_name_for’ from /Users/ragrawal/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.0/lib/active_record/associations/join_dependency.rb:17:in `initialize’ from /Users/ragrawal/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.0/lib/active_record/relation/finder_methods.rb:219:in `new’ from /Users/ragrawal/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.0/lib/active_record/relation/finder_methods.rb:219:in `construct_join_dependency_for_association_find’ from /Users/ragrawal/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.0/lib/active_record/relation/finder_methods.rb:192:in `exists?’ from /Users/ragrawal/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.0/lib/active_record/validations/uniqueness.rb:32:in `validate_each’ from /Users/ragrawal/.rvm/gems/ruby-1.9.2-p290/gems/activemodel-3.2.0/lib/active_model/validator.rb:153:in `block in validate’ from /Users/ragrawal/.rvm/gems/ruby-1.9.2-p290/gems/activemodel-3.2.0/lib/active_model/validator.rb:150:in `each’ […]

使用方法:使用grouped_collection_select选择

是否有可能以某种方式使用:selected选项,您可以在带有grouped_collection_select函数的普通选择视图助手上使用? 我想设置在我的列表中预先选择的值。 我试过传递:选择没有运气的选项! 这是我测试的一些代码片段: grouped_collection_select ‘user[subscription_attributes]’, :subscription_plan_id, Trade.order(:name).all, :subscription_plans, :name, :id, :display_name, { :include_blank => true, :selected => 5 } grouped_collection_select ‘user[subscription_attributes]’, :subscription_plan_id, Trade.order(:name).all, :subscription_plans, :name, :id, :display_name, :include_blank => true, :selected => 5 两个版本都不起作用。 没有选择设置。 我正在使用它为嵌套模型设置一个值。 我正在使用railscasts动态选择列表方法: http ://railscasts.com/episodes/88-dynamic-select-menus-revised 我无法使用formtastic与组选择很好地玩,所以我必须手动完成,但是当用户validation失败时我不会保留此值。 我想在修复validation错误时保留此设置。

ruby on rails模型validation的浮点精度

我正在尝试使用正则表达式validation美元金额: ^[0-9]+\.[0-9]{2}$ 这样可以正常工作,但每当用户提交表单并且美元金额以0(零)结束时,ruby(或rails?)会将0关闭。 因此500.00变为500.0因此无法进行正则表达式validation。 有没有办法让ruby / rails保持用户输入的格式,而不管尾随的零?

Rails模型中的条件validation

我有一个Rails 3.2.18应用程序,我正在尝试对模型进行一些条件validation。 在呼叫模型中有两个字段:location_id(它是与预定义位置列表的关联)和:location_other(这是一个文本字段,其中某人可以键入字符串或在这种情况下是地址)。 我希望能够做的是在创建对以下位置的调用时使用validation:location_id或:location_other被validation存在。 我已经阅读了Railsvalidation指南并且有点困惑。 希望有人可以通过条件轻松地阐明如何轻松完成这项工作。

Railsvalidation以确保用户名不会与现有路由冲突?

我想确保用户无法创建与我现有路由冲突的用户名。 我也希望能够拒绝我可能定义的未来路线。 我想要完成这样的事情: 在模型中: class User @@invalid_usernames } end 在一些初始化程序中: User.invalid_usernames += Rails.application.routes.routes.map(&:path).join(“\n”).scan(/\s\/(\w+)/).flatten.compact.uniq 这是“Rails方式”吗? 有没有更好的办法?

跳过Rails 3中嵌套属性的validation?

我目前正在使用嵌套的表单/ fields_for,我想知道是否有一种简单的方法可以跳过嵌套属性的validation? 我可以在某个地方挤压一个object.nested_object.save(:validate => false)吗?

Rails – 枚举字段的测试validation

我正在使用Rails 4枚举,我想正确测试它们,所以我为枚举字段设置了这些测试: it { should validate_inclusion_of(:category).in_array(%w[sale sale_with_tax fees lease tax_free other payroll]) } it { should validate_inclusion_of(:type).in_array(%w[receivable payable]) } 这是他们正在validation的模型: class Invoice < ActiveRecord::Base belongs_to :user enum category: [:sale, :sale_with_tax, :fees, :lease, :tax_free, :other, :payroll] enum type: [:receivable, :payable] validates :user, presence: true validates :issue_date, presence: true validates :series, presence: true validates :folio, presence: true […]

覆盖SecurePassword的validation错误消息

可能重复: 使用Rails完全自定义validation错误消息 我正在玩Rails 3.1rc1并认为SecurePassword对我有用。 但是,我不喜欢默认的错误信息, Password digest can’t be blank 。 如果我自己调用validates_presence_of :password_digest我可以传递:message => “Password can’t be blank”但因为它在框架中我不知道如何覆盖消息以删除“digest”这个词只会混淆joe用户。 有人知道怎么做吗? 编辑: 尝试添加’覆盖’: validates_presence_of如下: class User “Password can’t be blank” end 但是当尝试提交一个空白密码时,你只会得到两倍的错误: 表格无效 密码摘要不能为空 密码摘要密码不能为空

Rails 3.0中的默认validation错误消息在哪里?

Rails 3.0中的默认validation错误消息在哪里? 例如,ActiveRecord :: Error.default_error_messages [:take]的等价物是什么? 我已经发现ActiveModel处理错误而不是ActiveRecord,但我自己找不到错误。

如何在rails中指定和validation枚举?

我目前有一个模型Attend将有一个状态列,这个状态列只有几个值。 STATUS_OPTIONS = {:yes,:no,:maybe} 1)我不确定如何在用户插入Attend之前validation这一点? 基本上java中的枚举,但我怎么能在rails中这样做?