Forum: Ruby on Rails How to redisplay submitted data on validation error?

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Szymon N. (Guest)
on 2006-03-23 18:21

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
Chris H. (Guest)
on 2006-03-23 19:35
(Received via mailing list)
render :action overrides the default render for the current action with
render of another action.
redirect_to effectively sends a header to the browser telling it to load
new URL (controller/action) specified in the redirect_to call.  you
have any access to the data that was used in the action unless its in

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
in the view.  since it's an 'empty' model, the fields are empty.  the
then fills out the form and submits to the create action.  the create
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
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
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
automatically hilight the fields with errors for you (see comments below

class ExampleController < ApplicationController
  def new
    @model =

  def create
    @model =[:model]
      redirect_to :action => :list
      render_action => :new


<%= form_start_tag :action => :create %>
    <label for="model_email"><%= 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
either include the scaffold.css file or define .fieldWithErrors yourself
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
some style definitions found in scaffold.css.

hope this helps.

Szymon N. (Guest)
on 2006-03-23 19:54

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).
Szymon N. (Guest)
on 2006-03-23 20:44
I've created ContactForm model (using this tutorial:
and added email verification to it.

Now redisplaying submitted data works and the code is simpler than
before :)
This topic is locked and can not be replied to.