# How to redisplay submitted data on validation error?

**URL:** <https://www.ruby-forum.com/t/how-to-redisplay-submitted-data-on-validation-error/55760>\
**Category:** Rails\
**Created:** [March 23, 2006, 5:21pm UTC](https://www.ruby-forum.com/t/how-to-redisplay-submitted-data-on-validation-error/55760 "2006-03-23T17:21:23Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![g0nzo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/3bc359/32.png) [@g0nzo](https://www.ruby-forum.com/u/g0nzo)\
**Post date:** [March 23, 2006, 5:21pm UTC](https://www.ruby-forum.com/t/how-to-redisplay-submitted-data-on-validation-error/55760/1 "2006-03-23T17:21:23Z")

</div>

Hi!

If i have an object it’s easy.  
But i’m making a simple contact form and i check if email address is  
correct. If it’s not i redisplay the form with the info that there was  
an error (should i use redirect\_to or render :action? what’s the  
difference?), but all previously submitted data is lost.  
I tried creating variables using names of the form fields, but it didn’t  
work.

---

<div class="post-metadata">

**Author:** ![g0nzo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/3bc359/32.png) [@g0nzo](https://www.ruby-forum.com/u/g0nzo)\
**Post date:** [March 23, 2006, 6:54pm UTC](https://www.ruby-forum.com/t/how-to-redisplay-submitted-data-on-validation-error/55760/2 "2006-03-23T18:54:46Z")

</div>

Thanks!

It cleared up few things for me.

However if i don’t have any model for my contact form and i’d like to  
redisplay submitted data, what then? Can submitted data be redisplayed  
without using a model? Or should i just create a model for it? I’m using  
the submitted data only to send it by email, i don’t store them  
anywhere. But it looks like creating a model would really simplify  
things (like validation).

---

<div class="post-metadata">

**Author:** ![g0nzo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/3bc359/32.png) [@g0nzo](https://www.ruby-forum.com/u/g0nzo)\
**Post date:** [March 23, 2006, 6:35pm UTC](https://www.ruby-forum.com/t/how-to-redisplay-submitted-data-on-validation-error/55760/3 "2006-03-23T18:35:32Z")

</div>

render :action overrides the default render for the current action with  
the  
render of another action.  
redirect\_to effectively sends a header to the browser telling it to load  
a  
new URL (controller/action) specified in the redirect\_to call. you  
won’t  
have any access to the data that was used in the action unless its in  
the  
session.

to explain what is happening below, when you go to the new action, rails  
creates an empty model. that model’s data is then used to populate the  
form  
in the view. since it’s an ‘empty’ model, the fields are empty. the  
user  
then fills out the form and submits to the create action. the create  
action  
creates a model from the submitted data and attempts to save it. if the  
save is successful, the browser is redirected to the list action. if  
the  
save is unsuccessful, the view for the new action is rendered. in this  
case, we have a model instance loaded with the submitted data, so when  
the  
new form is displayed, the data from the model is used to populate the  
form. the form helpers are smart and know if a model has errors and  
will  
automatically hilight the fields with errors for you (see comments below  
example).

class ExampleController \< ApplicationController  
def new  
@model = Model.new  
end

def create  
@model = Model.new(@params[:model]  
if @model.save  
redirect\_to :action =\> :list  
else  
render\_action =\> :new  
end  
end  
end

new.rhtml

\<%= form\_start\_tag :action =\> :create %\>  
\<%= text\_field :model, :email %\>  
\<%= submit\_tag “Submit” %\>  
\<%= form\_end\_tag %\>

By using the helper methods this way (ie, text\_field as opposed to  
text\_field\_tag), Rails will automatically repopulate the fields with the  
data that was submitted and hilight the fields with errors. you’ll need  
to  
either include the scaffold.css file or define .fieldWithErrors yourself  
for  
the hilight to work.

in addition to this, you can use error\_messages\_for(:model) to provide a  
descriptive message as to why the fields have errors. this also relies  
on  
some style definitions found in scaffold.css.

hope this helps.

Chris

---

<div class="post-metadata">

**Author:** ![g0nzo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/3bc359/32.png) [@g0nzo](https://www.ruby-forum.com/u/g0nzo)\
**Post date:** [March 23, 2006, 7:44pm UTC](https://www.ruby-forum.com/t/how-to-redisplay-submitted-data-on-validation-error/55760/4 "2006-03-23T19:44:20Z")

</div>

I’ve created ContactForm model (using this tutorial:  
[http://rails.techno-weenie.net/tip/2005/11/19/validate\_your\_forms\_with\_a\_table\_less\_model](http://rails.techno-weenie.net/tip/2005/11/19/validate_your_forms_with_a_table_less_model))  
and added email verification to it.

Now redisplaying submitted data works and the code is simpler than  
before 🙂
