We've moved discussions to Discord

Error: Unpermitted parameter: :audio_files

Brian Carpenter
I have a form that lets users select multiple audio files to upload to the DB for some additional server-side processing.

Each selected audio file has associated attributes and for every audio file selected, I need to create a new entry into my AudioLoad model.

Responding to button 'onclick' event listener, I loop through each file in the in files array and
1) build/append a formData() object with required information
2) post this information using Rails.ajax

I can post all the data successfully if I DONT include the file object.

When I do include the file object in the post, I get the error unpermitted parameter: audio_file

I've included the console output below in addition to the code.


Model
class AudioLoad < ApplicationRecord

  belongs_to :user
  belongs_to :account
  has_many_attached :audio_files

end


Controller

...
def audio_load_params
      params.require(:audio_load).permit(:user_id, :account_id, :file_name, :file_hash, :load_date, :load_status, :tag_type, audio_files: [])
    end
...

View
<div class="container mx-auto my-8 px-4">
  <div class="max-w-3xl mx-auto">
    <div class="flex justify-between items-center mb-4">
      <h1 class="h3">New Audio Load</h1>
      <%= link_to 'Cancel', audio_loads_path, class: "btn btn-link" %>
    </div>

    <div class="bg-white rounded shadow p-8">
      <%= render 'form', audio_load: @audio_load %>
    </div>

    <div id="filelist">

    </div>
    
  </div>
</div>


Form
<%= form_with(model: @audio_load) do |form| %>
  <%= collection_select :audio_load, :account_id, Account.where(owner_id: current_user.id).order(:name), :id, :name, include_blank: false, class: "form-control" %>
  <%= form.file_field :audio_files, multiple: true, id: "upload", accept: "audio/*" %>
  <%= form.hidden_field :user_id, value: current_user.id %>
  <button id="tag-file" class="btn-primary">Tag File</button>
<% end %>

Relevant JS
......
  for (var i = 0; i < files.length; ++i) {

        data = new FormData()
        data.append("audio_load[user_id]", userId)
        data.append("audio_load[account_id]", accountId)
        data.append("audio_load[file_name]", files[i].name)
        data.append("audio_load[load_status]", 'ready')
        data.append("audio_load[audio_files]", [files[i]])

              Rails.ajax({
                  url: "/audio_loads",
                  type: "POST",
                  data: data,
                  dataType: 'script',
                  success: function(response) {
                    displayTagStatus(response)
                    }
                })

          }
        }
......


Console Output

Started POST "/audio_loads" for ::1 at 2020-10-26 13:16:28 -0400
Processing by AudioLoadsController#create as JS
  Parameters: {"audio_load"=>{"user_id"=>"1", "account_id"=>"40", "file_name"=>"[FILTERED]", "load_status"=>"ready", "audio_files"=>#<ActionDispatch::Http::UploadedFile:0x00007fcf1a6c0700 @tempfile=#<Tempfile:/var/folders/r4/c0h7ky9j6xv4dks8j8csvgwr0000gn/T/RackMultipart20201026-36315-drtnxt.wav>, @original_filename="bc_guitarfuzz_hh.wav", @content_type="audio/wav", @headers="Content-Disposition: form-data; name=\"audio_load[audio_files]\"; filename=\"bc_guitarfuzz_hh.wav\"\r\nContent-Type: audio/wav\r\n">}}
  User Load (0.4ms)  SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["id", 1], ["LIMIT", 1]]
  ↳ app/controllers/concerns/set_current_request_details.rb:11:in `block (2 levels) in <module:SetCurrentRequestDetails>'
  Account Load (0.6ms)  SELECT "accounts".* FROM "accounts" INNER JOIN "account_users" ON "accounts"."id" = "account_users"."account_id" WHERE "account_users"."user_id" = $1 AND "accounts"."id" = $2 LIMIT $3  [["user_id", 1], ["id", 40], ["LIMIT", 1]]
  ↳ app/controllers/concerns/set_current_request_details.rb:24:in `block (2 levels) in <module:SetCurrentRequestDetails>'
Unpermitted parameter: :audio_files
   (0.2ms)  BEGIN
  ↳ app/controllers/audio_loads_controller.rb:26:in `create'
  User Load (0.3ms)  SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2  [["id", 1], ["LIMIT", 1]]
  ↳ app/controllers/audio_loads_controller.rb:26:in `create'
  Account Load (0.3ms)  SELECT "accounts".* FROM "accounts" WHERE "accounts"."id" = $1 LIMIT $2  [["id", 40], ["LIMIT", 1]]
  ↳ app/controllers/audio_loads_controller.rb:26:in `create'
  AudioLoad Create (1.1ms)  INSERT INTO "audio_loads" ("user_id", "account_id", "file_name", "load_status", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id"  [["user_id", 1], ["account_id", 40], ["file_name", "bc_guitarfuzz_hh.wav"], ["load_status", "ready"], ["created_at", "2020-10-26 17:16:28.070039"], ["updated_at", "2020-10-26 17:16:28.070039"]]
  ↳ app/controllers/audio_loads_controller.rb:26:in `create'
  AudioLoad Load (0.3ms)  SELECT "audio_loads".* FROM "audio_loads" WHERE "audio_loads"."load_status" = $1  [["load_status", "ready"]]
  ↳ app/models/audio_load.rb:15:in `tag_audio'
  AudioLoad Update All (0.9ms)  UPDATE "audio_loads" SET "load_status" = $1, "tag_type" = $2 WHERE "audio_loads"."id" IN (SELECT "audio_loads"."id" FROM "audio_loads" WHERE "audio_loads"."id" = $3 LIMIT $4)  [["load_status", "completed"], ["tag_type", "duplicate"], ["id", 64], ["LIMIT", 1]]
  ↳ app/models/audio_load.rb:21:in `block in tag_audio'
   (3.2ms)  COMMIT
  ↳ app/controllers/audio_loads_controller.rb:26:in `create'
Completed 200 OK in 41ms (Views: 0.5ms | ActiveRecord: 9.4ms | Allocations: 15337)




Chris Oliver
For array params, you must have empty square brackets at the end so Rails knows to parse it as an array. 👍

```
data.append("audio_load[audio_files][]", files[i])
```
Notifications
You’re not receiving notifications from this thread.