# Why doesn't my user-entered data persist after validation/preview in my multi-model form?

**URL:** <https://www.ruby-forum.com/t/why-doesnt-my-user-entered-data-persist-after-validation-preview-in-my-multi-model-form/136334>\
**Category:** Rails\
**Created:** [April 30, 2008, 10:07pm UTC](https://www.ruby-forum.com/t/why-doesnt-my-user-entered-data-persist-after-validation-preview-in-my-multi-model-form/136334 "2008-04-30T22:07:02Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![ebrad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/13edae/32.png) [@ebrad](https://www.ruby-forum.com/u/ebrad)\
**Post date:** [April 30, 2008, 10:07pm UTC](https://www.ruby-forum.com/t/why-doesnt-my-user-entered-data-persist-after-validation-preview-in-my-multi-model-form/136334/1 "2008-04-30T22:07:02Z")

</div>

I’m creating a timesheet application where a “Timesheet” object has 7  
“TimesheetDays” objects. Everything works fine until error validation  
or a ‘preview’ (after calculations) when the form is re-rendered. The  
data in the fields for the Timesheet object persist after render, but  
all my text input fields for each of the TimesheetDay objects reset. I  
know I’m doing something wrong here, but I need the data the use  
entered into the TimsheetDay fields to remain. Please help!!

### VIEW - Timesheet Form

# New timesheet

\<% form\_tag :action =\> ‘create’ do %\>

\<% if params[:calculate] %\>

\<%= @timesheet.total\_week\_hours %\>

 \<%= submit\_tag "Save and Finish" %\>
\<% end %\> \<%= error\_messages\_for 'timesheet' %\>

Week: \<%= select 'timesheet', 'week', [["Beginning #{@last\_week.to\_s(:long)} (Last Week)", @last\_week], ["Beginning #{@this\_week.to\_s(:long)} (This Week)", @this\_week] ] %\>

\<% 7.times do |d| %\> \<%= render :partial =\> 'days', :locals =\> { :day =\> d += 1 } %\> \<% end %\>

| Day | Time In | Time Out | | Time In | Time Out | | PTO | Vacation | Holiday | Jury | Bereav. | Ext PTO | Unpaid | | Comments |
| --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- |
|
| |

\<%= submit\_tag “Calculate”, :name =\> ‘calculate’ %\>  
\<% end %\>

## CONTROLLER - Timesheet

…  
def create  
week\_select  
@timesheet = Timesheet.new(params[:timesheet])  
if params[:calculate]  
run\_calculations  
render :action =\> ‘new’  
elsif !@timesheet.save  
render :action =\> ‘new’  
elsif @timesheet.save  
# some stuff…  
end  
…  
end

---

<div class="post-metadata">

**Author:** ![ebrad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/13edae/32.png) [@ebrad](https://www.ruby-forum.com/u/ebrad)\
**Post date:** [May 1, 2008, 12:02am UTC](https://www.ruby-forum.com/t/why-doesnt-my-user-entered-data-persist-after-validation-preview-in-my-multi-model-form/136334/2 "2008-05-01T00:02:03Z")

</div>

make sure you’re creating an @timesheet each time

---

<div class="post-metadata">

**Author:** ![ebrad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/13edae/32.png) [@ebrad](https://www.ruby-forum.com/u/ebrad)\
**Post date:** [April 30, 2008, 10:28pm UTC](https://www.ruby-forum.com/t/why-doesnt-my-user-entered-data-persist-after-validation-preview-in-my-multi-model-form/136334/3 "2008-04-30T22:28:09Z")

</div>

same question i asked, but i couldn’t state it properly. asked the  
question twice.  
i have tried several things but can not get it to work.

tried several ways of getting params data to a instance variable. i  
was using a fields\_for like this…  
\<% fields\_for “second\_model”, @second\_info do | second\_form | %\>

---

<div class="post-metadata">

**Author:** ![ebrad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/13edae/32.png) [@ebrad](https://www.ruby-forum.com/u/ebrad)\
**Post date:** [May 1, 2008, 8:53pm UTC](https://www.ruby-forum.com/t/why-doesnt-my-user-entered-data-persist-after-validation-preview-in-my-multi-model-form/136334/4 "2008-05-01T20:53:28Z")

</div>

Yes, Ruby F., the data isn’t saved, but in “Preview Mode” for  
calculating totals before confirming. I don’t think that’s the problem  
because the Timesheet attributes persist, only the TimesheetDay object  
attributes are cleared. Maybe I need to add the user-entered data to a  
cookie or the session?

I’m tempted to just hard-wire the entire timesheet into a 1 week deal,  
but it seems hackish to me. I’m going to the Pragmatic Programmers  
Advanced Rails workshop next week. I’ll ask them and update my post.

---

<div class="post-metadata">

**Author:** ![ebrad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/13edae/32.png) [@ebrad](https://www.ruby-forum.com/u/ebrad)\
**Post date:** [May 1, 2008, 12:54am UTC](https://www.ruby-forum.com/t/why-doesnt-my-user-entered-data-persist-after-validation-preview-in-my-multi-model-form/136334/5 "2008-05-01T00:54:48Z")

</div>

This might be a stupid observation, but isn’t your code falling out to  
render :action =\> ‘new’ before save ever gets called?

def create  
week\_select  
@timesheet = Timesheet.new(params[:timesheet])  
if params[:calculate]  
run\_calculations  
render :action =\> ‘new’ #code is falling out here and never  
running save  
elsif !removed\_email\_address@domain.invalid  
render :action =\> ‘new’  
elsif @timesheet.save  
# some stuff…  
end

---

<div class="post-metadata">

**Author:** ![ebrad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/13edae/32.png) [@ebrad](https://www.ruby-forum.com/u/ebrad)\
**Post date:** [May 1, 2008, 8:58pm UTC](https://www.ruby-forum.com/t/why-doesnt-my-user-entered-data-persist-after-validation-preview-in-my-multi-model-form/136334/6 "2008-05-01T20:58:09Z")

</div>

Hi Roger,  
I’m wondering if you mean create a new a @timesheet\_day (a.k.a Build)?  
If so, maybe that is my problem.

This is a visualization of my model relationships:

-Timesheet (main object where weekly totals are stored)  
—TimesheetDay (for sun)  
—TimesheetDay (for mon)  
—TimesheetDay (for tues)  
—TimesheetDay (for wed)  
—TimesheetDay (for thurs)  
—TimesheetDay (for fri)  
—TimesheetDay (for sat)

---

<div class="post-metadata">

**Author:** ![ebrad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/13edae/32.png) [@ebrad](https://www.ruby-forum.com/u/ebrad)\
**Post date:** [May 10, 2008, 10:37am UTC](https://www.ruby-forum.com/t/why-doesnt-my-user-entered-data-persist-after-validation-preview-in-my-multi-model-form/136334/7 "2008-05-10T10:37:36Z")

</div>

k the problem was with the default values…they werer overriding the  
entered data upon re-rendering. I removed the defaults and it works.

---

<div class="post-metadata">

**Author:** ![ebrad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/13edae/32.png) [@ebrad](https://www.ruby-forum.com/u/ebrad)\
**Post date:** [May 1, 2008, 10:32pm UTC](https://www.ruby-forum.com/t/why-doesnt-my-user-entered-data-persist-after-validation-preview-in-my-multi-model-form/136334/8 "2008-05-01T22:32:01Z")

</div>

maek sure your day rendering partial is using the right days [?]

 \<%= render :partial =\> 'days', :locals =\> { :day =\> d += 1 } %\>
