Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow attaching multiple files to a nested field #631

Open
bunnakal opened this issue Mar 21, 2024 · 2 comments
Open

Allow attaching multiple files to a nested field #631

bunnakal opened this issue Mar 21, 2024 · 2 comments

Comments

@bunnakal
Copy link

bunnakal commented Mar 21, 2024

Hi there,

This gem was awesome and helped me a lot with dynamic nested fields.

Currently, I try to implement such nested fields or records that contain file fields that need to have multiple = true.

I have three model here rfq model contain multiple rfq_item and rfq_item has multiple rfq_item_attachment.

I have my nested fields as below HTML code:

%tr.nested-fields{"data-controller" => "select2 rfq flatpickr"}  
  %td
    = f.input :item_description, label: false
  %td
    = f.input :link_ref, label: false
  %td
    = f.input :quantity, label: false
  %td
    = f.association :unit_of_measure, value_method: :id, label_method: :unit_of_measure, include_blank: true, label: false
  -# %td
  -#   = f.text_field :deliver_by_date, placeholder: "select date..", class: "form-control bg-transparent datepicker"
  %td 
    = f.input :requester_recommended_vendor, label: false
  %td
    -# = f.file_field :attachment, multiple: true, name: "rfq_items[attachment][]", label: false
    = f.fields_for :rfq_item_attachments do |att|
      = att.file_field :attchment, :multiple => true
  %td
    = link_to_remove_association '<i class="bi bi-trash"></i>'.html_safe, f, { data: {rfq_target: 'remove', action: "click->rfq#remove" }, class: "btn-sm btn-danger" }

Chrome inspects the element as below:

`

`

Parameter when form submit:

Parameters: {"authenticity_token"=>"[FILTERED]", "rfq"=>{"requested_by"=>"Bunna Kal", "department"=>"Technology", "need_by_date"=>"20-04-2024", "delivery_to"=>"IT department", "warranty_term_id"=>"1", "rfq_items_attributes"=>{"0"=>{"item_description"=>"Test", "link_ref"=>"fsfs", "quantity"=>"1.0", "unit_of_measure_id"=>"2", "requester_recommended_vendor"=>"Testing", "rfq_item_attachments"=>{"attchment"=>["", #<ActionDispatch::Http::UploadedFile:0x0000000141f339b8 @tempfile=#<Tempfile:/var/folders/8n/nscxqsgs50gddvmfjzvhw1100000gn/T/RackMultipart20240321-46633-9prdbi.jpeg>, @original_filename="bunna.jpeg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"rfq[rfq_items_attributes][0][rfq_item_attachments][attchment][]\"; filename=\"bunna.jpeg\"\r\nContent-Type: image/jpeg\r\n">, #<ActionDispatch::Http::UploadedFile:0x0000000141f338a0 @tempfile=#<Tempfile:/var/folders/8n/nscxqsgs50gddvmfjzvhw1100000gn/T/RackMultipart20240321-46633-ellga1.png>, @original_filename="LikeableTours.png", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"rfq[rfq_items_attributes][0][rfq_item_attachments][attchment][]\"; filename=\"LikeableTours.png\"\r\nContent-Type: image/png\r\n">]}, "_destroy"=>"false", "id"=>"13"}}, "status"=>"Created"}, "rfq_attachments"=>{"attachment"=>[""]}, "commit"=>"Update", "locale"=>"en", "id"=>"8"}

The error in console:

Unpermitted parameter: :rfq_item_attachments. Context: { controller: RfqsController, action: update, request: #<ActionDispatch::Request:0x0000000141f37400>, params: {"_method"=>"patch", "authenticity_token"=>"[FILTERED]", "rfq"=>{"requested_by"=>"Bunna Kal", "department"=>"Technology", "need_by_date"=>"20-04-2024", "delivery_to"=>"IT department", "warranty_term_id"=>"1", "rfq_items_attributes"=>{"0"=>{"item_description"=>"Test", "link_ref"=>"fsfs", "quantity"=>"1.0", "unit_of_measure_id"=>"2", "requester_recommended_vendor"=>"Testing", "rfq_item_attachments"=>{"attchment"=>["", #<ActionDispatch::Http::UploadedFile:0x0000000141f339b8 @tempfile=#<Tempfile:/var/folders/8n/nscxqsgs50gddvmfjzvhw1100000gn/T/RackMultipart20240321-46633-9prdbi.jpeg>, @original_filename="bunna.jpeg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"rfq[rfq_items_attributes][0][rfq_item_attachments][attchment][]\"; filename=\"bunna.jpeg\"\r\nContent-Type: image/jpeg\r\n">, #<ActionDispatch::Http::UploadedFile:0x0000000141f338a0 @tempfile=#<Tempfile:/var/folders/8n/nscxqsgs50gddvmfjzvhw1100000gn/T/RackMultipart20240321-46633-ellga1.png>, @original_filename="LikeableTours.png", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"rfq[rfq_items_attributes][0][rfq_item_attachments][attchment][]\"; filename=\"LikeableTours.png\"\r\nContent-Type: image/png\r\n">]}, "_destroy"=>"false", "id"=>"13"}}, "status"=>"Created"}, "rfq_attachments"=>{"attachment"=>[""]}, "commit"=>"Update", "locale"=>"en", "controller"=>"rfqs", "action"=>"update", "id"=>"8"} }

I am not really sure what to add in controller to allow this rfq_item_attachment. I am using Carrierwave.

My controller parameter controller:

def rfq_params
    params.require(:rfq).permit(:requested_by, :department, :need_by_date, :user_id, :status, :total_rfq_qty, :delivery_to, :warranty_term_id, :supplier_id, :new_supplier,
    rfq_items_attributes: [:id, :item_description, :unit_of_measure_id,:quantity, :deliver_by_date, :link_ref, :requester_recommended_vendor, :attachment, :status, :_destroy, :rfq_item_attachments],
    rfq_attachments_attributes: [:id, :rfq_id, :attachment])
  end

So, what would be I have missing here? You can also see the form below:

I am waiting to hearing from you very soon.

Image 21-3-24 at 10 53 in the morning

@nathanvda
Copy link
Owner

I think you should fix your rfq_params like this:

def rfq_params
    params.require(:rfq).permit(
        :requested_by, :department, :need_by_date, :user_id, :status, :total_rfq_qty, :delivery_to, 
        :warranty_term_id, :supplier_id, :new_supplier,
        rfq_items_attributes: [
              :id, :item_description, :unit_of_measure_id,:quantity, :deliver_by_date, :link_ref, 
              :requester_recommended_vendor, :attachment, :status, :_destroy, 
              :rfq_item_attachments: [:id, :rfq_id, :attachment]
        ]
    )
  end

Mmmmm but do you have the accepts_nested_attributes also in the RfqItem model?

@bunnakal
Copy link
Author

Hello @nathanvda ,

Thank you for your prompt reply here but I still have no luck to make this getting to work as exspected.

I have created a simple Rails app (Rails 7); please have a look here and suggest what should be the correct approach.

I am waiting to hearing from you very soon.

Best,
Bunna

@bunnakal bunnakal changed the title Possibly add multiple attachment file to a nested field Allow attaching multiple files to a nested field Apr 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants