views:

60

answers:

2

I'm trying to use some validation only if a specific method in my controller is being called:

validates_presence_of :reasons, :on => :update_description

However I get this error:

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'

Am I going about this the wrong way? Basically I have a multi-page form and I've broken up the pages into multiple update methods which they submit. In this case I'm updating the registration object using a method I've defined called update_description. I only want the validation to occur when this method is called. Possible?

Update: Adding error line:

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

The :on parameter specifies when this validation is active (default is :save, other options :create, :update). This is relative to the model, not the controller.

JRL
ahhhh I see. I was a little unclear about that. Would you happen to know a good way to do validation for a model that is being completed across multiple pages? I thought I could create custom methods similar to save, create, update, etc. and then use that to specify when the validation is active, but I think I understand the difference now.
Jeff
A: 

It looks like you want a wizard plugin. The two that I know of are:

  1. acts_as_wizard
  2. wizardly

Hopefully these should get you started.

jonnii