# Validates\_presence\_of and a blank form

**URL:** <https://www.ruby-forum.com/t/validates-presence-of-and-a-blank-form/150553>\
**Category:** Rails\
**Created:** [October 9, 2008, 8:25pm UTC](https://www.ruby-forum.com/t/validates-presence-of-and-a-blank-form/150553 "2008-10-09T20:25:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![justinbankston](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9dc877/32.png) [@justinbankston](https://www.ruby-forum.com/u/justinbankston)\
**Post date:** [October 9, 2008, 8:25pm UTC](https://www.ruby-forum.com/t/validates-presence-of-and-a-blank-form/150553/1 "2008-10-09T20:25:35Z")

</div>

Hey All,

I’m frustrated with validates\_presence\_of. I have a form, and I’m  
validating four fields using validates\_presence\_of…

It works fine, except that when the form comes back with the error  
messages, it is blank. If a user enters three of the four required  
fields and submits the form, they get back a blank form with an error  
message and have to re-enter everything.

It seems to me like there must be an easy way to fix this, otherwise  
v\_p\_o is a total joke.

I’ve googled my head off to no good effect. Can anyone offer advice?

Thanks,  
JB

---

<div class="post-metadata">

**Author:** ![justinbankston](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9dc877/32.png) [@justinbankston](https://www.ruby-forum.com/u/justinbankston)\
**Post date:** [October 9, 2008, 9:07pm UTC](https://www.ruby-forum.com/t/validates-presence-of-and-a-blank-form/150553/2 "2008-10-09T21:07:53Z")

</div>

One way to make this happen is to do a redirect in the controller after  
Model.save or Model.update\_attributes rather than a render back to the  
new or edit action. Could that be your problem?

(If that’s not clear, post the update method of the appropriate  
controller.)

HTH,

-Roy

---

<div class="post-metadata">

**Author:** ![justinbankston](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9dc877/32.png) [@justinbankston](https://www.ruby-forum.com/u/justinbankston)\
**Post date:** [October 11, 2008, 1:42am UTC](https://www.ruby-forum.com/t/validates-presence-of-and-a-blank-form/150553/3 "2008-10-11T01:42:59Z")

</div>

To add to the previous post…make sure that you label you form  
correctly, for example:  
\<% form\_for(@person) do |f| %\>  
\<%= f.error\_messages %\>

\<%= f.label :name%\>  
 \<%= f.text\_field :name %\>

\<%= f.submit "Create" %\>

\<% end %\>

in the code above, if the @person has a name, the text\_field is then  
populated with that value. or what you could do is get the choice made  
by the user i.e.: \<% selected\_choice =  
@survey.get\_choice\_for\_question(@question) %\>  
end if it’s not nil display it as follows: \<% selected\_choice\_id =  
selected\_choice.id unless selected\_choice.nil? %\>

(i have a random number of questions so i use a loop to display each  
one and get the appropriate answers and such so you have to adapt my  
code to what you have)

I had the same problem and this fixed it for me, so i hope it helps.  
if not, show us your code and we’ll see how we can help.
