I'm trying to save a record of a class being added to a sc开发者_运维技巧hedule, then take the start date and add a week at a time adding additional entries into the database until I reach the end date. Here is my controller method
def schedule_class
@program_schedule = ProgramSchedule.new(params[:program_schedule])
if @program_schedule.save
@program_schedule.schedule_classes
end
end
So if I successfully save the model I call this method in the model
def schedule_classes
class_start = Date.parse(self.first_date, '%m/%d/%Y')
until class_start > self.last_date.to_date
schedule = Schedule.new
schedule.program_schedule_id = self.id
schedule.start_date = class_start.to_s + ' ' + self.start_time
schedule.end_date = class_start.to_s +' ' + self.end_time
schedule.deleted = false
schedule.save
class_start.to_time.advance(:week => 1).to_date
end
end
The error I get is $_ value need to be String (nil given)
I get that error on this line: class_start = Date.parse(self.first_date, '%m/%d/%Y')
And these are the parameters in program_schedule
{"commit"=>"Schedule Class",
"authenticity_token"=>"MRtbXnPNq/xLr74awXfS4ksG/NK92aLwsogC8R8IHB0=",
"utf8"=>"✓",
"program_schedule"=>{"program_id"=>"1",
"end_time"=>"01:00 am",
"last_date"=>"08/25/2011",
"first_date"=>"07/21/2011",
"week_day_id"=>"5",
"start_time"=>"12:00 am"}}
Since the model has all it's values I'm not sure where the nil value error is coming from.
I fixed it using the following
class_start = DateTime.parse(self.first_date.to_date.to_s + ' ' + self.start_time.strftime('%I:%M%p'))
Since they were datetime objects I needed to strip the date out of one and the time out of the other to create the actual datetime I needed.
精彩评论