Tag: validation

rails中的交叉唯一性模型validation

我有一个模型,我希望列包含不同的ID。 因此,用户可以跟随另一个用户,但不跟随他们自己。 移民 class CreateFollows < ActiveRecord::Migration def change create_table :follows do |t| t.integer :follower_id t.integer :followee_id t.timestamps end end end 模型 class Follow < ActiveRecord::Base belongs_to :follower, class_name: 'User' belongs_to :followee, class_name: 'User' validates :follower_id, uniqueness: { scope: :followee_id } end 但我的测试似乎失败了 测试 it ‘cannot follow itself’ do user = FactoryGirl.create(:user) follow = FactoryGirl.create(:follow, […]

如何正确地将record.image_integrity_error添加到现有条件?

历史: 如何与carrierwave结合有条件地显示状态validation? 我正在使用这个gem构建一个对象: https : //github.com/schneems/wicked/wiki/Building-Partial-Objects-Step-by-Step validates :name, :presence => true, :if => :active_or_name? validates :details, :presence => true, :if => :active_or_details? validates :image, presence: true, image_size: { width: { min: 400 }, height: { min: 400 } }, :file_size => { :maximum => 5.megabytes.to_i }, if: :active_or_details? function: def active? status == ‘active’ end […]

validation失败时渲染/重定向到新操作(rails)

我有一个表单,用户可以输入isbn,它会尝试查找书籍数据并保存。 当isbn查找validation失败时(例如,如果有人输入错误),我希望它重定向到另一种forms,如果isbn查找失败,用户可以手动输入数据(但如果其他validation如数字价格失败则不会)。 关于如何做到这一点的任何想法? 谢谢您的帮助!

FactoryGirl循环依赖和validation

我是FactoryGirl的新手,在Ruby中,我正在尝试编写一些rspec但是我遇到了一些循环依赖的麻烦。 我搜索并尝试了几个东西,最后一个产生了无限循环,我不得不关闭我的WM。 (proc 100%) 车型/投票 class Vote < ActiveRecord::Base has_many :vote_options 车型/ vote_option class VoteOption < ActiveRecord::Base belongs_to :vote accepts_nested_attributes_for :vote_options, reject_if: :all_blank 规格/型号/ vote_spec describe Vote do describe ‘should return the good label’ do let(:nobody) { FactoryGirl.create(:vote, visibility: 0) } it “should not be visible” do nobody.intercom_custom_data[:visibility].should == I18n.t(“votes.new.visibility_none”) end end end 规格/工厂。 factory […]

Rails 3:未知的validation器:’PresenceValidator’(ArgumentError)

Rails 3.2.19,Ruby 1.9.3p547。 在为开发添加一些gems( validates :some_field, some_default_rails_validator: true及其各种依赖项)之后,表单中的任何validation都会validates :some_field, some_default_rails_validator: true失败。 在捆绑之前,所有这些validation都非常有效。 回滚Gemfile并重新捆绑无效。 其他人似乎遇到了自定义(或拼写错误)validation器的问题,但失败都是针对Rails默认validation器的。 例如,运行rails c给了我: Users/MY_USER/.rvm/gems/ruby-1.9.3-p547@MY_APP/gems/activemodel-3.2.19/lib/active_model/validations/validates.rb:96 in ‘rescue in block in validates’: Unknown validator: ‘PresenceValidator’ (Argument Error) 追溯到 from /Users/MY_USER/MY_APP/app/models/document.rb:4 in ” 在模型文件中,我有: validates :title, presence: true PresenceValidator是一个默认的Railsvalidation器,不知何故gem似乎已经开始了。 我不确定该怎么做(除了从太空中取消应用程序并重新初始化我的开发环境)。

Rails 3 – validation时间存在

我有一个带有Time属性的模型。 我想检查时间不能为空(更好的选择可能是检查输入是时间,但我不知道如何处理)。 我试过这个validation: # id :integer not null, primary key # school_class_id :integer # meeting_time :time class Meeting { :message => “can’t be empty!” } end 然后我试图在规范中检查这个,这失败了(空的时间还可以,但它不应该是)。 我做错了什么? Rspec的: # id :integer not null, primary key # school_class_id :integer # meeting_time :time require ‘spec_helper’ describe Meeting do before(:each) do @class = FactoryGirl.create( :school_class ) @attr_meeting […]

有很多创造! validation存在

我有两个模型User和Item ,它们与has_many through关联相关。 我想创建没有项目的用户,但项目创建应validation至少一个用户的存在。 我用以下方式创建项目: @user.items.create!(name: “Ball”) 在创建项目之前,如何创建用户在场的工作validation? 我尝试了以下方法: validate :users, presence: true Item模型中为validate :users, presence: true 在Item模型中validate :item_users, presence: true 在ItemUser连接模型中validate :user, :item, presence: true error.add(:base, ‘select at least one user’) if self.users.count < 1 ,则在Item模型中validate :should_have_at_least_one_user error.add(:base, ‘select at least one user’) if self.users.count < 1 ,其中私有函数执行error.add(:base, ‘select at least one user’) if […]

如何使用Nokogiri使用本地dtd文件正确validationxml文件?

我有一个简单,有效的DTD和一个似乎符合DTD的有效XML文件,但Nokogiri正在生成大量validation输出,这意味着XML文件未通过validation。 dtd文件是: xml文件是: FOO SOFTWARE. The core global object. This is a special singleton object. It is used for internal Wayland protocol features. The sync request asks the server to emit the ‘done’ event on the returned wl_callback object. Since requests are handled in-order and events are delivered in-order, this can be used as a […]

在保存在rails中之前将输入转换为整数

我有一组表示用户生日的选择输入:birthyear,birthmonth和birthday。 我想validation这样的birthyear: validates_inclusion_of :birthyear, :in => Date.today.year-50..Date.today.year-12 因此,用户注册时可以至少12年但最多50年。 我的问题是来自输入的变量是一个字符串而不是整数。 那么如何将输入转换为整数? 有没有更简单的方法来检查用户年龄?

Rails validate_association与模型的错误消息

我使用模型中的validates_associated来使用其他模型的validation代码。 这个问题是validation失败的消息是“..无效”。 我想将模型validation失败的实际描述性错误冒出来! 我发现了这个问题: 与模型的错误消息相关的validation 这看起来像一个非常接近的解决方案: module ActiveRecord module Validations class AssociatedBubblingValidator value)) end end end end end module ClassMethods def validates_associated_bubbling(*attr_names) validates_with AssociatedBubblingValidator, _merge_attributes(attr_names) end end end end 但实际上它遇到了一个错误: undefined method `valid?’ for # 任何人都可以帮助完成这个几乎工作的工作!? 完整的错误跟踪是: undefined method `valid?’ for # Extracted source (around line #6): 4 5 6 7 8 9 def validate_each(record, […]