未定义的方法`with_indifferent_access'for

时间:2015-02-25 07:47:02

标签: ruby-on-rails ruby hash

在我的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

任何想法?

1 个答案:

答案 0 :(得分:2)

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

我想你正在尝试做的是在shared_incident_params运行时扩展drive_off_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