Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 获取参数的问题_Ruby On Rails_Ruby_Ruby On Rails 3 - Fatal编程技术网

Ruby on rails 获取参数的问题

Ruby on rails 获取参数的问题,ruby-on-rails,ruby,ruby-on-rails-3,Ruby On Rails,Ruby,Ruby On Rails 3,我使用activerecord将一些时间表对象分组在一起 scope :grouped_by_user_with_total_time, lambda { group(:user_id, :day).select('user_id, SUM(time_worked) AS time_total, day, editable, approvable, accepted, comments') } 在我这样做之后,现在当我尝试对分组对象调用approve方法时,我得到了错误 Couldn't

我使用activerecord将一些时间表对象分组在一起

scope :grouped_by_user_with_total_time, lambda {
  group(:user_id, :day).select('user_id, SUM(time_worked) AS time_total, day, editable, approvable, accepted, comments')
}
在我这样做之后,现在当我尝试对分组对象调用approve方法时,我得到了错误

Couldn't find TimeSheet without an ID
它突出了我方法中的第三行

def approve
@time_sheets = []
*t = TimeSheet.find(params[:time_sheets])**  
if t.instance_of?(Array)
  @time_sheets = t
else
  @time_sheets << t
end
successful = []
unsuccessful = []

@time_sheets.each do |timesheet|
  unless timesheet.approved
    timesheet.approve
    if timesheet.save
      successful << timesheet
    else
      unsuccessful << timesheet
    end
  end
end

我对此不是很有经验,我不知道如何检查我的参数[:time_sheets]是否为零,这可能是问题所在。非常感谢您的帮助。

是参数[:时间表]传递为nil将导致此错误

def approve
if params[:time_sheets]
@time_sheets = []
*t = TimeSheet.find(params[:time_sheets])**  
if t.instance_of?(Array)
  @time_sheets = t
else
  @time_sheets << t
end
successful = []
unsuccessful = []

@time_sheets.each do |timesheet|
  unless timesheet.approved
    timesheet.approve
    if timesheet.save
      successful << timesheet
    else
      unsuccessful << timesheet
    end
  end
end
end

通过添加带有if params[:time_sheets]的检查,我们可以确保该方法仅在params[:time_sheets]不是nil时运行。然而,似乎您在传递参数时遇到了问题。如果您将代码张贴在显示参数传递位置的位置,我们可以找到它传递零的原因。

现在我找不到id=approve的时间表这意味着参数[:time_sheets]传递字符串“approve”,显示您设置参数的位置的代码[:time_sheets]为什么对此投反对票?请始终对否决票发表评论,谢谢