4

我有一个“运行”对象,其中可以容纳许多“层”。Runs 接受层的嵌套属性,但 rails 无法确保嵌套对象的参数在各种坏事发生之前是有效的。我正在尝试检查任何图层上的“deposition_source_id”参数是否为空。如何循环遍历图层?
此行可以从 ONE 或任何层获取参数。“0”指定层。
params[:run][:layers_attributes]["0"][:deposition_source_id]

如何检查每一层?这些是参数:

    => {"utf8"=>"✓",
 "_method"=>"put",
 "authenticity_token"=>"T+X6sSda5vV19hpMZEAdf5RWSKPhJrm/q9+NXxTC5G8=",
 "run"=>
  {"number"=>"31310.0",
   "start_time_string"=>"08/08/2012 at 11:08 AM",
   "system_id"=>"4",
   "technician_id"=>"4",
   "duration"=>"",
   "base_pressure"=>"",
   "platters"=>"Apples",
   "overcoats_string"=>"",
   "planetary"=>"",
   "layers_attributes"=>
    {"0"=>
      {"deposition_source_id"=>"",
       "material_id"=>"60",
       "lot_id"=>"118",
       "thickness_goal"=>"32",
       "measured_thickness"=>"3",
       "tooling_factor"=>"",
       "sensor_number"=>"",
       "xtal_life"=>"",
       "release_agent"=>"0",
       "_destroy"=>"false",
       "id"=>"3401"}}},
 "commit"=>"Update Run",
 "action"=>"update",
 "controller"=>"runs",
 "id"=>"2319"}
4

1 回答 1

6

你可以这样做:

params[:run][:layers_attributes].each do |layer_number, params|
  # do your validation here on params[:deposition_source_id]
end

如果您要做的只是确保 deposit_source_id 始终为空,我会这样做:

deposition_source_id_is_empty = true
params[:run][:layers_attributes].each do |layer_number, params|
  if params[:deposition_source_id].present?
    deposition_source_id_is_empty = false
    break
  end
end
if deposition_source_id_is_empty
  # do the things you'd do here if the parameters pass validation
end
于 2012-08-09T17:06:20.383 回答