0

I want to access usr_additional_users_attributes in the following params? 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

any suggestions ?

1
  • I would say, go step by step to check params[:usr_contact], then params[:usr_contact][:usr_contact_vendors_attributes], and so on. One of these is nil. Commented May 3, 2017 at 8:34

4 Answers 4

2

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. By using dig, it's like it's checking every attribute before you're accessing it. It would be good to check inside the item too with 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
Sign up to request clarification or add additional context in comments.

Comments

0

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. are nested one.Have a try

Comments

0

You need to make sure that your item is not nil before pushing it to your array. Easiest way is to do this is by adding a check if item is nil.

@emailSet << item[:email] unless item.nil?

Comments

0

I fixed it by printing params. It was something like this

{"0"=>{"usr_vendor_property_attributes"=>{"shop_name"=>"cbhcvbcvb", "specified_area"=>"cbcvbcb", "website"=>"[email protected]", "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. 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

1 Comment

Yes, but if one of the params in the middle is nil, it will raise an error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.