Forum: Ruby on Rails Form fields cleared out because of validation

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.
9b08e89cdce5a02bc9c2222825a14a67?d=identicon&s=25 kai (Guest)
on 2007-05-03 08:04
(Received via mailing list)
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 =
    when :post
      client =[:client])
      if true
        flash[:notice] = "Client successfully added"
        redirect_to({:controller => 'client', :action => 'overview'},
        flash[:errobj] = 'client'

### Layout Code
 <% if flash[:errobj] -%>
   <%= error_messages_for flash[:errobj] -%>
 <% end %>
9b08e89cdce5a02bc9c2222825a14a67?d=identicon&s=25 kai (Guest)
on 2007-05-07 20:01
(Received via mailing list)
Okay, I am an idiot. But there is still hope because I found out

Instead of

      client =[:client])

I should have said

      @client =[:client])

821395fe70906c8290df7f18ac4ac6cf?d=identicon&s=25 Rick Olson (Guest)
on 2007-05-07 20:14
(Received via mailing list)
> ### 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[:errobj], or @errobj.

Rick Olson
This topic is locked and can not be replied to.