1
我試圖保存一個正在添加到日程安排中的類的記錄,然後將開始日期添加到數據庫中,直到我到達結束日期爲止。這裏是我的控制器方法Rails:解析日期問題
def schedule_class
@program_schedule = ProgramSchedule.new(params[:program_schedule])
if @program_schedule.save
@program_schedule.schedule_classes
end
end
所以,如果我成功地挽救我的模型
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
調用此方法的模型我得到的錯誤是$ _值必須是字符串(零給出)
我得到的錯誤在這條線:class_start = Date.parse(self.first_date, '%M /%d /%Y')
而這些都是在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"}}
由於模型具有所有值,我不確定nil值錯誤來自哪裏。
以下可以幫助你 http://stackoverflow.com/questions/6475013/parsing-extracting-text-from-string-in-rails –
你存儲'first_date'爲日期或字符串? – drummondj
看起來像我將它作爲日期時間存儲在數據庫中。 – Jhorra