Refreshing RHTML page with previously posted data?

All,

After a POST, I want to be able to redisplay a view with the data that
was previously entered when my controller action notices a validation
failure and calls render on the view that does the submit.

Example:

View x.rhtml - text field is filled in
Call controller action
Controller action fails validation calls render (:action => ‘x’)
x.rthml shows up with blank text field which I would like to be filled
in.

Why this doesn’t work makes sense to me (more or less) already.

My text_field is not associated with an attribute on a model object.
The object and field names are arbitrary since I was able to
successfully pull the data from the param array.

Do I need to associate my form field with a model object in order to
successfully redisplay data that was submitted on a previous POST?

Is there another way to handle this by using text_field_tag?

I am wondering whether I should build a model just to handle this form
interaction or whether I should use a text_field_tag instead.

I am starting to see that thinking of model as strictly objects that
require persistence to a database is limiting :). Any takers for that
statement?

Any advice?

Thanks,
Wes

you don’t need to create a new model. you can save the value as an
instance variable and set that value back into the view. something like:

controller:

def new
@text_value = “”
end

def create
@text_value = params[:text_value]
end

view (_form.rhtml):

<input type=“text name=“text_value” value=”<%=h @text_value %>" />

i guess this way you won’t be able to use the text_field_tag…

-tak.

Takashi,

Thanks, that’s very helpful. This of course makes sense.

In fact, you can use text_field if you want since the first two params
are object name and attribute and you can still get at those via
params[‘object name’][‘attribute’].

You could do something like this

text_field(‘object’, ‘attr’, { :value => #{@text_value} }

as long as you set @text_value somewhere in your controller similar to
what is below and only address the parameter using
@params[‘object’][‘attr’].

However, this is very misleading to someone coming along later because
they will probably think that you are updating an instance of a model
named @object, when in fact ‘object’ is just a hash name.

So I went ahead with the straight input field until such time as I think
I should have a full-fledged model object backing my form.

Thanks for the help!

Wes

Takashi Okamoto wrote:

you don’t need to create a new model. you can save the value as an
instance variable and set that value back into the view. something like:

controller:

def new
@text_value = “”
end

def create
@text_value = params[:text_value]
end

view (_form.rhtml):

<input type=“text name=“text_value” value=”<%=h @text_value %>" />

i guess this way you won’t be able to use the text_field_tag…

-tak.