使用 JavaScript 将数组和文件对象提交到 Rails 后端

Using JavaScript to submit an array and a file object to a Rails backend

我无法弄清楚如何让我的 JavaScript 以 Rails 在我尝试使用文件参数编辑游戏时将接受的格式发送请求,并且相同负载中的数组参数。

Rails 控制器看起来像这样(明显地简化了):

class GamesController < ApplicationController
  def update
    @game = Game.find(params[:id])
    authorize @game

    respond_to do |format|
      if @game.update(game_params)
        format.html { render html: @game, success: "#{@game.name} was successfully updated." }
        format.json { render json: @game, status: :success, location: @game }
      else
        format.html do
          flash.now[:error] = "Unable to update game."
          render :edit
        end
        format.json { render json: @game.errors, status: :unprocessable_entity }
      end
    end
  end

  private

  def game_params
    params.require(:game).permit(
      :name,
      :cover,
      genre_ids: [],
      engine_ids: []
    )
  end
end

所以我 JavaScript 是这样的:

// this.game.genres and this.game.engines come from
// elsewhere, they're both arrays of objects. These two
// lines turn them into an array of integers representing
// their IDs.
let genre_ids = Array.from(this.game.genres, genre => genre.id);
let engine_ids = Array.from(this.game.engines, engine => engine.id);

let submittableData = new FormData();
submittableData.append('game[name]', this.game.name);
submittableData.append('game[genre_ids]', genre_ids);
submittableData.append('game[engine_ids]', engine_ids);
if (this.game.cover) {
  // this.game.cover is a File object
  submittableData.append('game[cover]', this.game.cover, this.game.cover.name);
}

fetch("/games/4", {
  method: 'PUT',
  body: submittableData,
  headers: {
    'X-CSRF-Token': Rails.csrfToken()
  },
  credentials: 'same-origin'
}).then(
  // success/error handling here
)

当我点击表单中的提交按钮时,JavaScript 运行,并且应该将数据转换为 Rails 后端可以接受的格式。不幸的是,我无法让它工作。

在没有图像文件可提交的情况下,我可以使用 JSON.stringify() 而不是 FormData 来提交数据,如下所示:

fetch("/games/4", {
  method: 'PUT',
  body: JSON.stringify({ game: {
    name: this.game.name,
    genre_ids: genre_ids,
    engine_ids: engine_ids
  }}),
  headers: {
    'Content-Type': 'application/json',
    'X-CSRF-Token': Rails.csrfToken()
  },
  credentials: 'same-origin'
})

这很好用。但是我一直无法弄清楚如何在提交 File 对象时使用 JSON.stringify 。或者,我可以使用 FormData 对象,它适用于简单的值,例如name,以及 File 对象,但不适用于像 ID 数组这样的数组值。

仅使用 ID 数组(使用 JSON.stringify)的成功表单提交在 Rails 控制台中如下所示:

Parameters: {"game"=>{"name"=>"Pokémon Ruby", "engine_ids"=>[], "genre_ids"=>[13]}, "id"=>"4"}

但是,我当前的代码以类似这样的形式结束:

Parameters: {"game"=>{"name"=>"Pokémon Ruby", "genre_ids"=>"18,2,15", "engine_ids"=>"4,2,10"}, "id"=>"4"}

Unpermitted parameters: :genre_ids, :engine_ids

或者,如果您在此过程中还上传了一个文件:

Parameters: {"game"=>{"name"=>"Pokémon Ruby", "genre_ids"=>"13,3", "engine_ids"=>"5", "cover"=>#<ActionDispatch::Http::UploadedFile:0x00007f9a45d11f78 @tempfile=#<Tempfile:/var/folders/2n/6l8d3x457wq9m5fpry0dltb40000gn/T/RackMultipart20190217-31684-1qmtpx2.png>, @original_filename="Screen Shot 2019-01-27 at 5.26.23 PM.png", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"game[cover]\"; filename=\"Screen Shot 2019-01-27 at 5.26.23 PM.png\"\r\nContent-Type: image/png\r\n">}, "id"=>"4"}

Unpermitted parameters: :genre_ids, :engine_ids

TL;DR:我的问题是,如何将这个有效负载(名称字符串、ID 数组以及游戏封面图像)发送到 Rails 使用 JavaScript?什么格式会被实际接受,我该如何实现?


Rails 应用程序是开源的,如果有帮助的话,you can see the repo here. The specific files mentioned are app/controllers/games_controller.rb and app/javascript/src/components/game-form.vue,尽管我已经针对这个问题大大简化了这两个应用程序。

您可以将 File 对象转换为 data URL 并将该字符串包含在 JSON 中,请参阅 处的 processFiles 函数或使用 [= JavaScript Array 上的 15=] 并将其设置为 FormData 对象的值,而不是将 Array 作为值传递给 FormData.

submittableData.append('game[name]', JSON.stringify(this.game.name));
submittableData.append('game[genre_ids]', JSON.stringify(genre_ids));
submittableData.append('game[engine_ids]', JSON.stringify(engine_ids));

我发现我可以使用 ActiveStorage's Direct Upload feature 来做到这一点。

在我的 JavaScript:

// Import DirectUpload from ActiveStorage somewhere above here.
onChange(file) {
  this.uploadFile(file);
},
uploadFile(file) {
  const url = "/rails/active_storage/direct_uploads";
  const upload = new DirectUpload(file, url);

  upload.create((error, blob) => {
    if (error) {
      // TODO: Handle this error.
      console.log(error);
    } else {
      this.game.coverBlob = blob.signed_id;
    }
  })
},
onSubmit() {
  let genre_ids = Array.from(this.game.genres, genre => genre.id);
  let engine_ids = Array.from(this.game.engines, engine => engine.id);
  let submittableData = { game: {
    name: this.game.name,
    genre_ids: genre_ids,
    engine_ids: engine_ids
  }};

  if (this.game.coverBlob) {
    submittableData['game']['cover'] = this.game.coverBlob;
  }

  fetch(this.submitPath, {
    method: this.create ? 'POST' : 'PUT',
    body: JSON.stringify(submittableData),
    headers: {
      'Content-Type': 'application/json',
      'X-CSRF-Token': Rails.csrfToken()
    },
    credentials: 'same-origin'
  })
}

然后我发现,根据 DirectUpload 的工作方式,我可以将 coverBlob 变量发送到 Rails 应用程序,所以它只是一个字符串。超级简单。