# Form fields cleared out because of validation

**URL:** <https://www.ruby-forum.com/t/form-fields-cleared-out-because-of-validation/103235>\
**Category:** Rails\
**Created:** [May 3, 2007, 8:04am UTC](https://www.ruby-forum.com/t/form-fields-cleared-out-because-of-validation/103235 "2007-05-03T08:04:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [May 3, 2007, 8:04am UTC](https://www.ruby-forum.com/t/form-fields-cleared-out-because-of-validation/103235/1 "2007-05-03T08:04:57Z")

</div>

Hi there

Implementing my first “update” procedure worked fine.  
I took this as a template for a “create” procedure, but that does not  
work as expected: when I enter invalid data, the validation stops me  
from saving the data, that’s working, but all fields are cleared!

Furthermore, also the data is not saved, no error message is dislayed.

Advice is more then welcome!

### Controller Code:

def create\_client  
case request.method  
when :get  
@client = Client.new  
when :post  
client = Client.new(params[:client])  
if client.save true  
flash[:notice] = “Client successfully added”  
redirect\_to({:controller =\> ‘client’, :action =\> ‘overview’},  
true)  
else  
flash[:errobj] = ‘client’  
end  
end  
end

### Layout Code

\<% if flash[:errobj] -%\>  
\<%= error\_messages\_for flash[:errobj] -%\>  
\<% end %\>

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [May 7, 2007, 8:01pm UTC](https://www.ruby-forum.com/t/form-fields-cleared-out-because-of-validation/103235/2 "2007-05-07T20:01:45Z")

</div>

Okay, I am an idiot. But there is still hope because I found out  
myself.

Instead of

```
  client = Client.new(params[:client])

```

I should have said

```
  @client = Client.new(params[:client])

```

…

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [May 7, 2007, 8:14pm UTC](https://www.ruby-forum.com/t/form-fields-cleared-out-because-of-validation/103235/3 "2007-05-07T20:14:46Z")

</div>

> ### Layout Code
> 
> \<% if flash[:errobj] -%\>  
> \<%= error\_messages\_for flash[:errobj] -%\>  
> \<% end %\>

If things aren’t working the way you expected, debug your code, write  
tests, etc.

\<%= debug flash[:errobj] %\>  
\<%= flash.inspect %\>

Or, get a little acquainted with ruby-debug and just throw a \<%  
debugger -%\> in there.

Flash messages survive a request, like a redirect, but are not  
available in the current request. Since you just render the same  
template on invalid models, use flash.now[:errobj], or @errobj.

–  
Rick O.  
[http://lighthouseapp.com](http://lighthouseapp.com)  
[http://weblog.techno-weenie.net](http://weblog.techno-weenie.net)  
[http://mephistoblog.com](http://mephistoblog.com)
