title: How do I get values entered in my Rails to form to remain after an error occurs in the form?
body: <p>I'm using Rails 5.  I have a form with fields like the below</p>
  
  <pre><code>&lt;%= form_for(@user) do |f| %&gt;
    ...
    &lt;div class="profileField"&gt;
      &lt;%= f.label :first_name %&gt;&lt;br/&gt;
      &lt;div class="field"&gt;&lt;%= f.text_field :first_name, :size =&gt; 20, :class =&gt; 'textField', :tabIndex =&gt; '1' %&gt;&lt;/div&gt;
    &lt;/div&gt;
    ...
  
    &lt;div class="profileField"&gt;
      Birthday &lt;% if !@user.errors[:dob].empty? %&gt;&lt;span class="profileError"&gt; &lt;%= @user.errors[:dob].join(', ') %&gt;&lt;/span&gt;&lt;% end %&gt; &lt;br/&gt; 
      &lt;div class="field"&gt;
        &lt;%= f.text_field :dob_string, :value =&gt; f.object.dob_string , :size =&gt; "20", :class =&gt; 'textField', placeholder: 'MM/DD/YYYY', :tabIndex =&gt; 1 %&gt;
      &lt;/div&gt;  
    &lt;/div&gt;
  </code></pre>
  
  <p>When the form submits, it invokes this logic in my controller</p>
  
  <pre><code>  def update
      @user = current_user 
      @user.dob_string = user_params[:dob_string] 
      if !@user.errors.any? &amp;&amp; @user.update_attributes(user_params)
        last_page_visited = session[:last_page_visited]  
        if !last_page_visited.nil? 
          session.delete(:last_page_visited) 
        else
          flash[:success] = "Profile updated"
        end
        redirect_to !last_page_visited.nil? ? last_page_visited : url_for(:controller =&gt; 'races', :action =&gt; 'index') and return
      end
  
      @country_selected = !@user.address.nil? &amp;&amp; !@user.address.country.nil? ? @user.address.country : Country.cached_find_by_iso('US')
      @states = @country_selected.states.sort_by {|obj| obj.name}      
      render 'edit'
    end
  </code></pre>
  
  <p>My question is, if there is an error in my form, how do I get the original values that someone entered before submitting the form to remain instead of the values that were previuosly saved?  Right now, if an error occurs, all the values the user previously entered are replaced by what was saved before.</p>
  
  <p><b>Edit:</b></p>
  
  <p>The solution didn't work.  I have a "dob" field (which is a PostGres DATE column), and hwen I entered an invalid value (e.g. "1234") and clicked "Save", everything saved without an error being thrown.  Below is my user model.  I also added the definition of my date field in my view.</p>
  
  <pre><code>class User &lt; ActiveRecord::Base
    has_many :assignments
    has_many :roles, through: :assignments
    belongs_to :address, :autosave =&gt; true  #, dependent: :destroy 
  
    accepts_nested_attributes_for :address
  
    attr_accessor :dob_string 
  
    def dob_string
      @dob_string || (self.dob ? self.dob.strftime('%m/%d/%Y') : "")  
    end
  
    def dob_string=(dob_s)
      date = dob_s &amp;&amp; !dob_s.empty? ? Date.strptime(dob_s, '%m/%d/%Y') : nil 
      self.dob = date
    rescue ArgumentError
      errors.add(:dob, 'The birth date is not in the correct format (MM/DD/YYYY)')
      @dob_string = dob_s
    end
  
    def role?(role)
      roles.any? { |r| r.name.underscore.to_sym == role.to_s.underscore.to_sym }
    end
  
    def admin?
      role? "Admin"
    end
  
    def name
      name = first_name 
      if !first_name.nil?
        name = "#{name} "
      end
      "#{name}#{last_name}" 
    end
  </code></pre>