# Validate on create but I don't save to DB

**URL:** <https://www.ruby-forum.com/t/validate-on-create-but-i-dont-save-to-db/138479>\
**Category:** Rails\
**Created:** [May 23, 2008, 2:13pm UTC](https://www.ruby-forum.com/t/validate-on-create-but-i-dont-save-to-db/138479 "2008-05-23T14:13:10Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![comopasta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/59ef9b/32.png) [@comopasta](https://www.ruby-forum.com/u/comopasta)\
**Post date:** [May 23, 2008, 2:13pm UTC](https://www.ruby-forum.com/t/validate-on-create-but-i-dont-save-to-db/138479/1 "2008-05-23T14:13:10Z")

</div>

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?  
  
  
\<%= 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.

---

<div class="post-metadata">

**Author:** ![comopasta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/59ef9b/32.png) [@comopasta](https://www.ruby-forum.com/u/comopasta)\
**Post date:** [May 23, 2008, 2:37pm UTC](https://www.ruby-forum.com/t/validate-on-create-but-i-dont-save-to-db/138479/2 "2008-05-23T14:37:35Z")

</div>

> \<%= 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.

---

<div class="post-metadata">

**Author:** ![comopasta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/59ef9b/32.png) [@comopasta](https://www.ruby-forum.com/u/comopasta)\
**Post date:** [May 23, 2008, 2:56pm UTC](https://www.ruby-forum.com/t/validate-on-create-but-i-dont-save-to-db/138479/3 "2008-05-23T14:56:50Z")

</div>

Hi,

\<%= error\_messages\_for :user %\> didn’t help 8-(

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

Yep I search twice because I forgot to remove it from previous test,  
sorry.  
This is from the password controller, it sets some flags on the user  
object. But in practice takes care of triggering some email sending to  
users when they want to reset their password.

> Anyway, you can check validations with valid?

I was checking that but the problem is that I don’t know on which object  
run the valid? method.

Cheers.

---

<div class="post-metadata">

**Author:** ![comopasta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/59ef9b/32.png) [@comopasta](https://www.ruby-forum.com/u/comopasta)\
**Post date:** [May 23, 2008, 3:15pm UTC](https://www.ruby-forum.com/t/validate-on-create-but-i-dont-save-to-db/138479/4 "2008-05-23T15:15:58Z")

</div>

On 23 May 2008, at 13:13, comopasta Gr wrote:

> 1- Add model like this:
> 
> class Password \< ActiveRecord::Base  
> validates\_presence\_of :email, :on =\> :create  
> end

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

---

<div class="post-metadata">

**Author:** ![comopasta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/59ef9b/32.png) [@comopasta](https://www.ruby-forum.com/u/comopasta)\
**Post date:** [May 23, 2008, 3:40pm UTC](https://www.ruby-forum.com/t/validate-on-create-but-i-dont-save-to-db/138479/5 "2008-05-23T15:40:11Z")

</div>

Hi,

Then I either:

1- Create my own div with the error messages if the field is empty  
2- Learn how to trigger one of those system errors that the validations  
use

Any pointers for #2 are welcome.

But that explains a lot, thanks. I knew that not really creating any  
records could be an issue but I thought that on :create was directly  
related to the controller function. It doesn’t as you mentioned.

Regards.
