簡體   English   中英

Rails驗證allow_nil引發異常

[英]Rails validation allow_nil throws exception

我試圖確保Rails模型中的布爾值不能設置為nil。 這是我的模型:

class Response < ActiveRecord::Base
  attr_accessible :attending

  # validations
  validates :attending, :allow_nil => false
end

當我嘗試運行規格時,會得到以下提示:

/Users/helixed/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/activemodel-3.2.7/lib/active_model/validations/validates.rb:86:in `validates': You need to supply at least one validation (ArgumentError)
    from /Users/helixed/Dropbox/Development/current/rsvp/app/models/response.rb:5:in `<class:Response>'
    from /Users/helixed/Dropbox/Development/current/rsvp/app/models/response.rb:1:in `<top (required)>'
    from /Users/helixed/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/activesupport-3.2.7/lib/active_support/dependencies.rb:251:in `require'
    from /Users/helixed/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/activesupport-3.2.7/lib/active_support/dependencies.rb:251:in `block in require'
    from /Users/helixed/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/activesupport-3.2.7/lib/active_support/dependencies.rb:236:in `load_dependency'
    from /Users/helixed/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/activesupport-3.2.7/lib/active_support/dependencies.rb:251:in `require'
    from /Users/helixed/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/activesupport-3.2.7/lib/active_support/dependencies.rb:359:in `require_or_load'
    from /Users/helixed/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/activesupport-3.2.7/lib/active_support/dependencies.rb:502:in `load_missing_constant'
    from /Users/helixed/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/activesupport-3.2.7/lib/active_support/dependencies.rb:192:in `block in const_missing'
    from /Users/helixed/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/activesupport-3.2.7/lib/active_support/dependencies.rb:190:in `each'
    from /Users/helixed/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/activesupport-3.2.7/lib/active_support/dependencies.rb:190:in `const_missing'
    from /Users/helixed/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/activesupport-3.2.7/lib/active_support/inflector/methods.rb:230:in `block in constantize'
    from /Users/helixed/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/activesupport-3.2.7/lib/active_support/inflector/methods.rb:229:in `each'
    from /Users/helixed/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/activesupport-3.2.7/lib/active_support/inflector/methods.rb:229:in `constantize'
    from /Users/helixed/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/activesupport-3.2.7/lib/active_support/inflector/methods.rb:260:in `safe_constantize'
    from /Users/helixed/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/activesupport-3.2.7/lib/active_support/core_ext/string/inflections.rb:66:in `safe_constantize'
    from /Users/helixed/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/actionpack-3.2.7/lib/action_controller/metal/params_wrapper.rb:152:in `_default_wrap_model'
    from /Users/helixed/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/actionpack-3.2.7/lib/action_controller/metal/params_wrapper.rb:169:in `_set_wrapper_defaults'
    from /Users/helixed/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/actionpack-3.2.7/lib/action_controller/metal/params_wrapper.rb:133:in `inherited'
    from /Users/helixed/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/actionpack-3.2.7/lib/abstract_controller/railties/routes_helpers.rb:7:in `block (2 levels) in with'
    from /Users/helixed/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/actionpack-3.2.7/lib/action_controller/railties/paths.rb:7:in `block (2 levels) in with'
    from /Users/helixed/Dropbox/Development/current/rsvp/app/controllers/responses_controller.rb:1:in `<top (required)>'
    from /Users/helixed/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/activesupport-3.2.7/lib/active_support/dependencies.rb:251:in `require'
    from /Users/helixed/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/activesupport-3.2.7/lib/active_support/dependencies.rb:251:in `block in require'
    from /Users/helixed/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/activesupport-3.2.7/lib/active_support/dependencies.rb:236:in `load_dependency'
    from /Users/helixed/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/activesupport-3.2.7/lib/active_support/dependencies.rb:251:in `require'
    from /Users/helixed/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/activesupport-3.2.7/lib/active_support/dependencies.rb:359:in `require_or_load'
    from /Users/helixed/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/activesupport-3.2.7/lib/active_support/dependencies.rb:313:in `depend_on'
    from /Users/helixed/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/activesupport-3.2.7/lib/active_support/dependencies.rb:225:in `require_dependency'
    from /Users/helixed/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/railties-3.2.7/lib/rails/engine.rb:439:in `block (2 levels) in eager_load!'
    from /Users/helixed/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/railties-3.2.7/lib/rails/engine.rb:438:in `each'
    from /Users/helixed/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/railties-3.2.7/lib/rails/engine.rb:438:in `block in eager_load!'
    from /Users/helixed/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/railties-3.2.7/lib/rails/engine.rb:436:in `each'
    from /Users/helixed/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/railties-3.2.7/lib/rails/engine.rb:436:in `eager_load!'
    from /Users/helixed/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/railties-3.2.7/lib/rails/application/finisher.rb:53:in `block in <module:Finisher>'
    from /Users/helixed/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/railties-3.2.7/lib/rails/initializable.rb:30:in `instance_exec'
    from /Users/helixed/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/railties-3.2.7/lib/rails/initializable.rb:30:in `run'
    from /Users/helixed/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/railties-3.2.7/lib/rails/initializable.rb:55:in `block in run_initializers'
    from /Users/helixed/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/railties-3.2.7/lib/rails/initializable.rb:54:in `each'
    from /Users/helixed/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/railties-3.2.7/lib/rails/initializable.rb:54:in `run_initializers'
    from /Users/helixed/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/railties-3.2.7/lib/rails/application.rb:136:in `initialize!'
    from /Users/helixed/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/railties-3.2.7/lib/rails/railtie/configurable.rb:30:in `method_missing'
    from /Users/helixed/Dropbox/Development/current/rsvp/config/environment.rb:5:in `<top (required)>'
    from /Users/helixed/Dropbox/Development/current/rsvp/spec/spec_helper.rb:3:in `require'
    from /Users/helixed/Dropbox/Development/current/rsvp/spec/spec_helper.rb:3:in `<top (required)>'
    from /Users/helixed/Dropbox/Development/current/rsvp/spec/controllers/responses_controller_spec.rb:1:in `require'
    from /Users/helixed/Dropbox/Development/current/rsvp/spec/controllers/responses_controller_spec.rb:1:in `<top (required)>'
    from /Users/helixed/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/configuration.rb:780:in `load'
    from /Users/helixed/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/configuration.rb:780:in `block in load_spec_files'
    from /Users/helixed/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/configuration.rb:780:in `map'
    from /Users/helixed/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/configuration.rb:780:in `load_spec_files'
    from /Users/helixed/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/command_line.rb:22:in `run'
    from /Users/helixed/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/runner.rb:69:in `run'
    from /Users/helixed/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/runner.rb:8:in `block in autorun'

:allow_nil應該作為一些驗證的選項。 您單獨使用它是不正確的使用方式。

如果要驗證:attending為true,則可以通過以下方式進行:

validates :attending, :acceptance => { :accept => true }, :allow_nil => false

更新:

為了驗證:attending是布爾值(是true還是false),您可以使用:

 validates :attending, :inclusion => { :in => [true, false] }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM