[英]How to access nested attributes in ruby on rails?
I want to access usr_additional_users_attributes in the following params? 我想在以下参数中访问usr_additional_users_attributes? how can i do that ?
我怎样才能做到这一点 ?
params.require(:usr_contact).permit(:first_name, :last_name, :dob, :gender,
:mobile, :email, :password,
:password_confirmation,
:image,
usr_contact_vendors_attributes:
[:id, usr_vendor_property_attributes:
[:shop_name, :specified_area, :mobile, :website,
usr_vendor_branches_attributes:
[:address_line1, :address_line2, :city ,:zip_code ],
usr_additional_users_attributes:[:id,:email,:role, :_destroy]]] )
end 结束
I want to get it like this in my controller and put emails to an array. 我想在控制器中像这样将其发送到数组中。 this one showing an error
这个显示错误
params[:usr_contact][:usr_contact_vendors_attributes][:usr_vendor_property_attributes][:usr_additional_users_attributes].values.each do |item|
@emailSet << item[:email]
end
undefined method `[]' for nil:NilClass nil:NilClass的未定义方法“ []”
any suggestions ? 有什么建议么 ?
You would have to use dig because when you're trying to access one of these params, they're null, so Ruby raise this exception. 您将不得不使用dig,因为当您尝试访问这些参数之一时,它们为null,因此Ruby引发了此异常。 By using
dig
, it's like it's checking every attribute before you're accessing it. 通过使用
dig
,就像在访问属性之前先检查每个属性。 It would be good to check inside the item too with item[:email].present?
也可以使用
item[:email].present?
检查项目内部item[:email].present?
Your code would look like: 您的代码如下所示:
params.dig(:usr_contact, :usr_contact_vendors_attributes, :usr_vendor_property_attributes, :usr_additional_users_attributes).values.each do |item|
@emailSet << item[:email] if item[:email].present?
end
I have an example of how I did it in my scenario: 我有一个在我的场景中如何做到的例子:
def patient_params
params.require(:patient).permit(*permited_patient_attributes)
end
def permited_patient_attributes
[
:user_id,
:start,
patient_demographic_attributes: [
:id,
:patient_id,
:start_date,
:end_date,
:first_name,
:middle_name,
:last_name,
:date_of_birth,
:gender,
:ethnicity,
:marital_status],
patient_contact_attributes: [
:id,
:patient_id,
:street_address,
:extended_address,
:locality,
:region,
:postal_code,
:country],
patient_email_attributes: [
:id,
:email],
patient_mrn_attributes: [
:id,
:patient_id,
:mrn],
contact_telephones_attributes: [
:id,
:party_type,
:party_id,
:name,
:number,
:_destroy],
patient_physicians_attributes: [
:id,
:patient_id,
physician_id: []]
]
end
Here patient_demographic_attributes, patient_contact_attributes. 在这里,Patient_demographic_attributes,Patient_contact_attributes。 are nested one.Have a try
嵌套一个。尝试一下
You need to make sure that your item
is not nil before pushing it to your array. 您需要先确保
item
不为零,然后再将其推入数组。 Easiest way is to do this is by adding a check if item
is nil. 最简单的方法是通过添加
item
是否为nil的检查。
@emailSet << item[:email] unless item.nil?
I fixed it by printing params. 我通过打印参数来修复它。 It was something like this
是这样的
{"0"=>{"usr_vendor_property_attributes"=>{"shop_name"=>"cbhcvbcvb", "specified_area"=>"cbcvbcb", "website"=>"bcbc@gmail.com", "mobile"=>"3543243", "usr_vendor_branches_attributes"=>{"1493800739760"=>{"address_line1"=>"", "address_line2"=>"", "city"=>"", "zip_code"=>""}}, "usr_additional_users_attributes"=>{"1493800739774"=>{"email"=>"sdgdfgd", "role"=>"Role1", "_destroy"=>"false"}}}}} permitted: false>
I have miss that ["0"], that was the issue. 我想念那个[“ 0”],这就是问题所在。 Then params can access in the controller like this.
然后,参数可以像这样在控制器中访问。
@test = params[:usr_contact][:usr_contact_vendors_attributes]["0"][:usr_vendor_property_attributes][:usr_additional_users_attributes]
Thanks for helping :D 感谢您的帮助:D
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.