是否可以验证自定义方法?

时间:2010-01-29 16:13:18

标签: ruby-on-rails validation

我只是在调用控制器中的特定方法时才尝试使用某些验证:

validates_presence_of :reasons, :on => :update_description

但是我收到了这个错误:

TypeError in RegistrationsController#create

nil is not a symbol


/Library/Ruby/Gems/1.8/gems/activerecord-2.3.5/lib/active_record/validations.rb:586:in `send'
/Library/Ruby/Gems/1.8/gems/activerecord-2.3.5/lib/active_record/validations.rb:586:in `validates_presence_of'
/Users/blah/Desktop/testApp/app/models/registration.rb:6
/Library/Ruby/Gems/1.8/gems/activesupport-2.3.5/lib/active_support/dependencies.rb:380:in `load_without_new_constant_marking'
/Library/Ruby/Gems/1.8/gems/activesupport-2.3.5/lib/active_support/dependencies.rb:380:in `load_file'
/Library/Ruby/Gems/1.8/gems/activesupport-2.3.5/lib/active_support/dependencies.rb:521:in `new_constants_in'
/Library/Ruby/Gems/1.8/gems/activesupport-2.3.5/lib/active_support/dependencies.rb:379:in `load_file'
/Library/Ruby/Gems/1.8/gems/activesupport-2.3.5/lib/active_support/dependencies.rb:259:in `require_or_load'
/Library/Ruby/Gems/1.8/gems/activesupport-2.3.5/lib/active_support/dependencies.rb:425:in `load_missing_constant'
/Library/Ruby/Gems/1.8/gems/activesupport-2.3.5/lib/active_support/dependencies.rb:80:in `const_missing'
/Library/Ruby/Gems/1.8/gems/activesupport-2.3.5/lib/active_support/dependencies.rb:92:in `const_missing'
/Library/Ruby/Gems/1.8/gems/activesupport-2.3.5/lib/active_support/dependencies.rb:437:in `load_missing_constant'
/Library/Ruby/Gems/1.8/gems/activesupport-2.3.5/lib/active_support/dependencies.rb:96:in `const_missing'
/Users/blah/Desktop/testApp/app/controllers/registrations_controller.rb:81:in `create'
/Library/Ruby/Gems/1.8/gems/actionpack-2.3.5/lib/action_controller/base.rb:1331:in `send'

我是以错误的方式来做这件事的吗?基本上我有一个多页面的表单,我已经将页面分解为他们提交的多种更新方法。在这种情况下,我正在使用我定义的名为update_description的方法更新注册对象。我只希望在调用此方法时进行验证。可能的?

更新: 添加错误行:

 def create
    @registration = Registration.new(params[:registration]) //error is here
[nav logic] 
    end

2 个答案:

答案 0 :(得分:1)

:on参数指定此验证何时处于活动状态(默认为:save,其他选项:create,:update)。这与模型有关,而不是控制器。

答案 1 :(得分:1)

看起来你想要一个向导插件。我知道的两个是:

  1. acts_as_wizard
  2. wizardly
  3. 希望这些能让你开始。

相关问题