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

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
@timesheet = Timesheet.new(params[:timesheet])
if params[:calculate]
render :action => ‘new’
elsif !@timesheet.save
render :action => ‘new’
elsif @timesheet.save
# some stuff…


make sure you’re creating an @timesheet each time

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 | %>

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.

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

def create
@timesheet = Timesheet.new(params[:timesheet])
if params[:calculate]
render :action => ‘new’ #code is falling out here and never
running save
elsif [email protected]
render :action => ‘new’
elsif @timesheet.save
# some stuff…

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)

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

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

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