gpt4 book ai didi

ruby-on-rails - 未定义的方法 `with_indifferent_access' 为

转载 作者:数据小太阳 更新时间:2023-10-29 08:03:48 25 4
gpt4 key购买 nike

在我的 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 的返回值与 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

关于ruby-on-rails - 未定义的方法 `with_indifferent_access' 为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28713742/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com