Topic: Form Values Disappear

When I submit this one form on my site, if the values don't go through, the error message comes up, as you'd expect, listing what values were bogus, but the form is empty.  What is Rails' method for preserving those values in the form when the user is given a chance to modify them and resubmit?  Thanks!

Re: Form Values Disappear

Can you post your form? and controller action that the form submits to?

Re: Form Values Disappear

I'm doing something slightly tricky, in that I'm working with multiple models, so I'm not sure if that's part of it...here's a condensed version of what's going on.  Any obvious mistakes on my part?  If it helps to know, there's also a checkbox in this form and the model has a validates_acceptance_of :terms_of_service, :on => :create....and even when I check the box it throws "There were problems with the following fields: * Terms of service must be accepted".  I mention this because it may be another error altogether or it may be stemming from the same source.  If the latter, maybe it helps the diagnosis.  Any suggestions?  Thanks---



users_controller.rb
class UsersController < ApplicationController

  def new
    @user = User.new
    @user.authors.build
  end

  def create
    if @user.save
      flash[:notice] = "Account successfully created."
      redirect_to welcome_path
    else
      render :action => 'new'
    end
  end

------------
new.html.erb

        <% form_for :user, :url => users_path do |f| %>
            <p>
            <%= f.text_field :first_name %>
            </p>
           
            <% fields_for "user[author_attributes]", @user.author do |author_form| %>               
                <p>
                <%= author_form.text_field :permalink %>
                </p>
            <% end %>
            <p><%= submit_tag 'Sign Up'%></p>
        <% end %>

--------------

It does seem to work if I make all of the logic explicit:

<% form_for @user do |f| %>
    <%= f.text_field :first_name, :value =>
    params.nil? ? "" :
      params[:user].nil? ? "" :
        params[:user][:first_name].nil? ? "" :
          params[:user][:first_name] %>
<% end %>

but that seems crazy...not to mention hideous...there's got to be an easier way, right??

Re: Form Values Disappear

Ah - think you are missing the line that defines @user.
@user is not persisted between actions.

def create
  @user = User.new(params[:user])  ####### add this line
  if @user.save
    flash[:notice] = "Account successfully created."
    redirect_to welcome_path
  else
    render :action => 'new'
  end
end