Hi,
I’m trying to validate a invalid date like Feb 31, 1976 but I’m
getting a really weird error msg and I can’t figure out how to fix it.
I thought that using validates_multiparameter_assignments it would
handle any invalid date posted but it is not working. Everytime that I
try I get:
You have a nil object when you didn’t expect it!
The error occurred while evaluating nil.>
#{RAILS_ROOT}/app/models/spec.rb:19
#{RAILS_ROOT}/app/controllers/spec_controller.rb:16:in edit' /Applications/Locomotive2/Bundles/standardRailsMar2007.locobundle/i386/ bin/mongrel_rails:16:in
load’
/Applications/Locomotive2/Bundles/standardRailsMar2007.locobundle/i386/
bin/mongrel_rails:16
but birthdate is not nil!!
If I try with a valid date everything will work perfectly.
below I’m pasting my controller and view code
class Spec < ActiveRecord::Base
belongs_to :user
acts_as_ferret
ALL_FIELDS = %w(first_name last_name address phone gender birthdate
city state zip_code)
STRING_FIELDS = %w(first_name last_name address phone city state)
VALID_GENDERS = [“Male”, “Female”]
START_YEAR = 1900
END_YEAR = Date.today.year - 18
ZIP_CODE_LENGTH = 10
validates_presence_of :zip_code, :birthdate, :first_name, :last_name,
:phone, :city, :state, :country
validates_length_of STRING_FIELDS, :maximum => DB_STRING_MAX_LENGTH
validates_inclusion_of :gender, :in => VALID_GENDERS, :allow_nil =>
true, :message => “must be male or female”
validates_multiparameter_assignments :message => " is not a valid
date."
validates_each :birthdate do |record, attr, value|
puts value.class
record.errors.add attr, “is not a valid date. You must be at
least 18 years old to sign in.” if value > Date.new((Date.today.year -
18),(Date.today.month),(Date.today.day))
end
end
<% form_for :spec do |form| %>
<%= "edit #{@user.screen_name} profile" %><%= error_messages_for ‘spec’ %>
<%= field_for(“text”, form, “first_name”)%>
<%= field_for(“text”, form, “last_name”) %>
Gender:
<%= radio_button(:spec, :gender, “Male”) %> Male
<%= radio_button(:spec, :gender, “Female”) %> Female
Birthdate:
<%= date_select :spec, :birthdate,
:start_year => Spec::END_YEAR,
:end_year => Spec::START_YEAR,
:order => [:month, :day, :year] %>
<%= field_for(“text”, form, “phone”) %>
<%= field_for(“text”, form, “address”) %>
<%= field_for(“text”, form, “city”) %>
<%= field_for(“text”, form, “state”) %>
<%= field_for(“text”, form, “country”) %>
<%= field_for(“text”, form, “zip_code”, Spec::ZIP_CODE_LENGTH,
Spec::ZIP_CODE_LENGTH) %>
<%= submit_tag "Update", :class => "submit" %>
<% end %>
Thanks,
Thiago G.