# Repopulating form after validation error

**URL:** <https://www.ruby-forum.com/t/repopulating-form-after-validation-error/110322>\
**Category:** Rails\
**Created:** [July 25, 2007, 11:12am UTC](https://www.ruby-forum.com/t/repopulating-form-after-validation-error/110322 "2007-07-25T11:12:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![tomzoo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a88e4f/32.png) [@tomzoo](https://www.ruby-forum.com/u/tomzoo)\
**Post date:** [July 25, 2007, 11:12am UTC](https://www.ruby-forum.com/t/repopulating-form-after-validation-error/110322/1 "2007-07-25T11:12:03Z")

</div>

Hi,  
I have a contact form, if a user enters an invalid email I redirect  
them back to the form to correct it. I didn’t want users to have to  
re-enter all the data so I want to repopulate the valid fields for them.  
The only way I can currently get this to work is by using globals, which  
doesn’t feel right. Is there another way?

\<% form\_tag( :action =\> :send\_mail ) do %\>

| name: |
| \<%= text\_field\_tag :name, $name, :size =\> 25 %\> |

---

<div class="post-metadata">

**Author:** ![tomzoo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a88e4f/32.png) [@tomzoo](https://www.ruby-forum.com/u/tomzoo)\
**Post date:** [July 25, 2007, 11:15am UTC](https://www.ruby-forum.com/t/repopulating-form-after-validation-error/110322/2 "2007-07-25T11:15:12Z")

</div>

woops, didn’t mean to post just then. In the above code I did have  
this:

\<% form\_tag( :action =\> :send\_mail ) do %\>

which I thought would populate the field if the :name param was set but  
it wasn’t working like that. Am i missing something here?

Thanks  
T

| name: |
| \<%= text\_field\_tag :name, params[:name}, :size =\> 25 %\> |

---

<div class="post-metadata">

**Author:** ![tomzoo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a88e4f/32.png) [@tomzoo](https://www.ruby-forum.com/u/tomzoo)\
**Post date:** [July 26, 2007, 12:46am UTC](https://www.ruby-forum.com/t/repopulating-form-after-validation-error/110322/3 "2007-07-26T00:46:10Z")

</div>

that should work just fine provided you don’t do a redirect in your  
controller upon failure… you need something like

MyController

def send\_mail

```
if request.post?
  if everything validates etc
    flash[:notice] = 'Message sent'
    redirect_to :action => :message_sent
  else
    render :action => :contact_form
  end
end

```

end

end

note that if there was an error you render an action, not redirect to  
it. if you redirect you are responsible for preserving all params  
yourself.

On Jul 25, 5:15 am, Tom E. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![tomzoo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a88e4f/32.png) [@tomzoo](https://www.ruby-forum.com/u/tomzoo)\
**Post date:** [July 26, 2007, 1:34pm UTC](https://www.ruby-forum.com/t/repopulating-form-after-validation-error/110322/4 "2007-07-26T13:34:14Z")

</div>

Thanks Jeff, hadn’t realised I needed to use render there, I was doing a  
redirect. It works now and no nasty globals misuse.
