date_select validation of a invalid date


What I'm asking might be simple for some of you but I'm trying to fix
this for two days and I just can't figure out how to make this thing
work. I have a model with a date_select validation that is working
just fine except when I fill the form with a invalid date like Feb 30
1989. I thought the plugin validates_multiparameter_assignments would
catch this kind of invalid date but for some reason is just not
working. Below I'm pasting my model and controller code to see if
someone can help me.

class Spec < ActiveRecord::Base

  belongs_to :user

  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 = - 18

  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_presence_of :zip_code, :birthdate
  validates_multiparameter_assignments :message => " is not a valid
  validates_each :birthdate do |record, attr, value|
        record.errors.add attr, "is not a valid date. You must be at
least 18 years old to sign in." if value > -

class SpecController < ApplicationController

  before_filter :protect

  # Edit the user's spec.
  def edit
      @user = User.find(session[:user_id])
      @title = "aykal - edit #{@user.screen_name} profile"
      @user.spec ||=
      @spec = @user.spec
      if param_posted?(:spec)
          if @user.spec.update_attributes(params[:spec])
            flash[:notice] = "Changes saved."
            redirect_to :controller => "user", :action => "index"


Thiago Guerra

Oh, I forgot to send the error msg I'm getting and I also double
checked and it is not working with any date I try. It is not just with
the invalid ones...

NoMethodError in SpecController#edit

You have a nil object when you didn't expect it!
The error occurred while evaluating nil.>

What I don't get is that birthdate is not nil...


Parameters: {"commit"=>"Update", "spec"=>{"city"=>"ccc xxx",
"zip_code"=>"30350210", "birthdate(1i)"=>"1989", "country"=>"Brasil",
"gender"=>"Male", "birthdate(2i)"=>"10", "birthdate(3i)"=>"23",
"phone"=>"+55 31 94224242", "first_name"=>"Thiago Lemos",
"last_name"=>"Guerra", "address"=>"R. Jubtanga 377/01 - Sto Antonio",
"state"=>"Minas Gerais"}}

this is my view

<% form_for :spec do |form| %>
  <legend><%= "edit #{@user.screen_name} profile" %></legend>

  <%= error_messages_for 'spec' %>
    <%= field_for("text", form, "first_name")%>
    <%= field_for("text", form, "last_name") %>
    <div class="form_row">
      <label for="gender">Gender:</label>
      <%= radio_button(:spec, :gender, "Male") %> Male
      <%= radio_button(:spec, :gender, "Female") %> Female
    <div class="form_row">
      <label for="birthdate">Birthdate:</label>
      <%= 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,

    <%= submit_tag "Update", :class => "submit" %>
<% end %>