Why does this 'validate' method raise an ArgumentError?

People,

I can not get validates_with(helloworld-y) rails in my application to work. Read the “callbacks and validators” section of the original RoR manual site and look for stackoverflow, you haven’t found anything.

Here is a stripped-down version of the code that I received after removing everything that could fail.

class BareBonesValidator < ActiveModel::Validator
  def validate    
    # irrelevant logic. whatever i put here raises the same error - even no logic at all
  end
end

class Unvalidable < ActiveRecord::Base
  validates_with BareBonesValidator
end

Sounds like an example tutorial, right? They have a very similar snippet in the RoR guide . Then go to rails consoleand get an ArgumentError when checking for a new record:

ruby-1.9.2-p180 :022 > o = Unvalidable.new
 => #<Unvalidable id: nil, name: nil, created_at: nil, updated_at: nil> 
ruby-1.9.2-p180 :023 > o.save
ArgumentError: wrong number of arguments (1 for 0)
    from /Users/ujn/src/yes/app/models/unvalidable.rb:3:in `validate'
    from /Users/ujn/.rvm/gems/ruby-1.9.2-p180@wimmie/gems/activesupport-3.0.7/lib/active_support/callbacks.rb:315:in `_callback_before_43'

I know I'm missing something, but what?

(NB: In order not to put BareBonesValidatorin a separate file, I left it on top model/unvalidable.rb).

+3
2

validate ( ). , .

class BareBonesValidator < ActiveModel::Validator
  def validate(record)
    if some_complex_logic
      record.errors[:base] = "This record is invalid"
    end
  end
end

: .

+2

ArgumentError: wrong number of arguments (1 for 0) , validate 1, 0.

, validate, , :

class BareBonesValidator < ActiveModel::Validator
  def validate(record) #added record argument here - you are missing this in your code
    # irrelevant logic. whatever i put here raises the same error - even no logic at all
  end
end
+1

All Articles