【发布时间】:2013-12-22 16:24:04
【问题描述】:
我有两个模型:计划和任务。 Task belongs_to Schedule 和 Schedule has_many 任务。任务的形式是计划的嵌套形式。我正在尝试编写控制器代码或模型方法,将用户输入的日期时间(称为:time_frame)用于任务,如果该日期时间已经发生(过去),将发出通知并重定向。我已经尝试过几种方法,但没有一种方法有效。我尝试为 schedules#update 编写此控制器代码:
schedule_params[:tasks_attributes].each do |task|
if task[:time_frame] < DateTime.now
render 'update', :notice => 'You must pick a future time.'
end
end
这里是 schedule_params:
def schedule_params
params.require(:schedule).permit(:emp_accepts,
tasks_attributes: [:title, :content, :_destroy, :time_frame,
:complete_time])
end
但我收到了错误:
no implicit conversion of symbol to integer
我尝试在 Schedule 模型中编写这样的模型方法:
before_update :compare_datetimes
def compare_datetimes
puts 'before task is found'
self.tasks.each do |task|
puts 'here is the task'
if task.time_frame < DateTime.now
puts 'It is in the past'
end
end
end
'before task is found' 被放到服务器上,但是其他两个 put 都没有被执行。我该怎么做?
【问题讨论】:
标签: ruby-on-rails oop datetime model-view-controller ruby-on-rails-4