Validate on create but I don't save to DB

Hi,

I want to do a simple thing: Validate a form so it raises an error when
a field is empty. The thing is that I don't save the contents of that
form into DB.

What I've done is:

1- Add model like this:

class Password < ActiveRecord::Base
  validates_presence_of :email, :on => :create
end

2- The view is:

<%= error_messages_for :password %>
<% form_for :password, :url => { :action => 'create' } do |f| %>
     What was the email address used to create your account?<br/><br/>
     <%= f.text_field :email, :size => "50" %>
     <%= submit_tag 'Reset Password' %>
  <% end %>

3- The controller is:

...
def create
  @user = User.find_for_forget(params[:email])
  return unless request.post?
  if @user = User.find_for_forget(params[:email])
    @user.forgot_password
    @user.save
    redirect_to login_path
  else
    render :action => 'new'
  end
end

4- And my problem is that I get no errors (from the system) when I press
the button in the form. Shouldn't it validate it when the create method
is called?

I didn't generate the model I just added the password.rb to use the
validate utilities.

Again, I just want to see the error that rails has by default.

Thanks for any hints you might have.

<%= error_messages_for :password %>

there is no password object in your create action, maybe this should be

<%= error_messages_for :user %>

def create
  @user = User.find_for_forget(params[:email])
  return unless request.post?
  if @user = User.find_for_forget(params[:email])
    @user.forgot_password
    @user.save
    redirect_to login_path
  else
    render :action => 'new'
  end
end

can't see, what this has to do with the password. And why do you search
for the user twice?

Anyway, you can check validations with valid?

this runs validate and validate_on_create or validate_on_update and
returns true if no errors were added otherwise false.

This means 'run the validation when the object is created'. It has absolutely nothing to do with the fact that this might happen from a controller action called create (the model doesn't care in the least about this). Fundamentally though, you don't validate forms, you validate activerecord objects.

Fred