在我的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
任何想法?
答案 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