未定义的方法 `with_indifferent_access' 为

undefined method `with_indifferent_access' for

在我的 Rails 应用程序中,我试图合并一些参数:

def shared_incident_params
    params.require(:drive_off_incident).permit(:incident_time, :product,
      :amount_cents, :where_from, :where_to, car_attributes: [:brand_id,
      :model, :color, :body_type, :plates], witness_attributes: [:first_name, :last_name, :email, :phone],
      notes_attributes: [:id, :content])
  end

  def drive_off_incident_params
    shared_incident_params.merge(person_description_attributes: [:height,
      :age, :gender, :nationality, :features, :clothes])
  end

但是这段代码给我以下错误:

NoMethodError:
   undefined method `with_indifferent_access' for [:height, :age, :gender, :nationality, :features, :clothes]:Array

有什么想法吗?

您确定要将 shared_incident_params 的 return 值与 drive_off_incident_params 中的哈希合并吗?该值可能是一个 Parameters 对象,但您正在尝试将散列合并到其中。 Parameters 继承自 ActiveSupport::HashWithIndifferentAccess,它试图在合并时将另一个值强制转换为同一类型。

我猜你想做的是在 运行 drive_off_incident_params.

时扩展 shared_incident_params 中的规则

你有没有尝试过这样做:

def shared_incident_params
  params.require(:drive_off_incident).permit(*permitted_incident_params)
end

def permitted_incident_params
  [
    :incident_time, 
    :product,
    :amount_cents, 
    :where_from, 
    :where_to, 
    car_attributes: [:brand_id, :model, :color, :body_type, :plates], 
    witness_attributes: [:first_name, :last_name, :email, :phone],
    notes_attributes: [:id, :content]
  ]
end

def drive_off_incident_params
  shared_incident_params
  params.permit(
    person_description_attributes: [
      :height,
      :age, 
      :gender, 
      :nationality, 
      :features, 
      :clothes ]
  )
end