# Retain Form Values on reset

**URL:** <https://www.ruby-forum.com/t/retain-form-values-on-reset/184875>\
**Category:** Rails\
**Created:** [March 9, 2010, 8:02pm UTC](https://www.ruby-forum.com/t/retain-form-values-on-reset/184875 "2010-03-09T20:02:22Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ashwin\_V](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8e7dd6/32.png) [@Ashwin\_V](https://www.ruby-forum.com/u/Ashwin_V)\
**Post date:** [March 9, 2010, 8:02pm UTC](https://www.ruby-forum.com/t/retain-form-values-on-reset/184875/1 "2010-03-09T20:02:22Z")

</div>

I have a Login page created with 4 fields and a submit button for an  
application i am developing.

Username :  
Email address :  
Password :  
Confirm Password :

i am validating each field. so if the username is missing , when u hit  
submit button , there is a notice / warning that flashes on top  
So if i just enter Username and hit Submit, it should flash a message  
saying that "Email ID is missing " at the same time retaining the  
value in the Username field.  
But that isnt happening. It just resets the form with the notice  
message on top.

How do i make this happen.  
Also i am not using Databases. So i have activeRecord Disabled. i am  
using an set of external API’s to login / register to this  
application

Any help would be Greatly appreciated.

---

<div class="post-metadata">

**Author:** ![Ashwin\_V](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8e7dd6/32.png) [@Ashwin\_V](https://www.ruby-forum.com/u/Ashwin_V)\
**Post date:** [March 9, 2010, 9:02pm UTC](https://www.ruby-forum.com/t/retain-form-values-on-reset/184875/2 "2010-03-09T21:02:04Z")

</div>

Ashwin,

I had to do something similar in my app. What you can do is create an  
object that mimics ActiveRecord in your controller. By that, I mean  
the object should have methods with the same names as the attributes  
which return the attribute’s value. Then pass that to a form helper,  
and you should be golden.

Controller action:  
@user = User.new(params[:user]) # User is a class you’ll have to  
write

View template:  
\<% form\_for :user, @user, :url =\> {:action =\> ‘create’} do |f| %\>  
\<%= f.text\_field :username %\>  
…  
\<%= f.submit %\>  
\<% end %\>

The trick is that `f.text_field :username` will create a text field  
and set its value to what is returned by calling `@user.username`.  
You can read up more on form helpers here:

> **[ActionView::Helpers::FormHelper](https://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html)**
>
> Form helpers are designed to make working with resources much easier compared to using vanilla HTML.

I hope that helps,

~David
