0

我试图让 Uppy 使用 Rails 上传表单,但我无法正确设置它。生成的 params 散列缺少其初始:image对象。在典型的 Rails 表单中,我假设每当它绑定到模型实例时都会处理此问题。但我不确定如何用 Uppy 表单做类似的事情。

只是在这里大声思考,但也许这与我如何设置endpoint选项有关?

编辑:澄清一下,我只是想为开发环境设置它,localhost:3000暂时在本地使用和存储所有内容

这是“标准”Rails 表单(没有 uppy):

<%= form_for @image, html: { multipart: true } do |f| %>
  <%= render 'shared/error_messages', object: f.object %>

  <div class="field">
    <%= f.label :file %><br>
    <%= f.file_field :file %>
  </div>
  <div class="actions">
    <%= f.submit "Upload Images" %>
  </div>
<% end %>

及其产生的参数哈希:

=> <ActionController::Parameters {"utf8"=>"✓", "authenticity_token"=>"Cr9NAN6NE9JV6HrI7RE7EmjmxqnlfbWk+LT5k9yLuq1Sm0L9E/zJ/eXCDcjbyw7A6lUHP2LFBqbfjY+SWoeO+w==", "image"=>{"file"=>#<ActionDispatch::Http::UploadedFile:0x000055d297d74168 @tempfile=#<Tempfile:/tmp/RackMultipart20200328-12356-1qjsycd.png>, @original_filename="Screenshot from 2019-07-22 04-24-23.png", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"image[file]\"; filename=\"Screenshot from 2019-07-22 04-24-23.png\"\r\nContent-Type: image/png\r\n">}, "commit"=>"Upload Images", "controller"=>"images", "action"=>"create"} permitted: false>

这是我对 Uppy 形式的尝试:

<%= form_for @image, html: { id: "uppy-form" } do |f| %>
  <%= render 'shared/error_messages', object: f.object %>

  <%= f.hidden_field :file, multiple: true, name: "images[file]", value: @image.file_data,
    accept: 'image/jpg,image/jpeg,image/gif,image/png,image/tif,image/tiff', class: "upload-hidden" %>

  <%= hidden_field_tag :authenticity_token, form_authenticity_token, id: :form_token %>
<% end %>

<script>
  const XHRUpload = Uppy.XHRUpload

  var uppy = Uppy.Core()
    .use(Uppy.Dashboard, {
      inline: true,
      target: '#uppy-form'
    })
    .use(XHRUpload, {
      endpoint: '/images',
      fieldName: 'images[file]'
    })
    .setMeta({
      authenticity_token: $("#form_token").attr('value')
    })

  uppy.on('complete', (result) => {
    console.log(`Upload complete! We've uploaded these files:`, result.successful)
  })
</script>

调用时产生的错误$ params

Completed 400 Bad Request in 5328ms (ActiveRecord: 0.4ms | Allocations: 40383)
ActionController::ParameterMissing - param is missing or the value is empty: image:

和完整的参数

=> <ActionController::Parameters {"authenticity_token"=>"CYUz9AszYR4G0WEiI4bDoOlUyH4jFMzAVRf4vF6m6OJRoTwJxkK7Mbb7FiIVXPZya+cJ6KSsf8JyLo692KrctA==", "relativePath"=>"null", "name"=>"Screenshot from 2019-07-10 06-34-34.png", "type"=>"image/png", "exifdata"=>"[object Object]", "images"=>{"file"=>#<ActionDispatch::Http::UploadedFile:0x000055d2962c49d0 @tempfile=#<Tempfile:/tmp/RackMultipart20200328-12359-1d093y1.png>, @original_filename="Screenshot from 2019-07-10 06-34-34.png", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"images[file]\"; filename=\"Screenshot from 2019-07-10 06-34-34.png\"\r\nContent-Type: image/png\r\n">}, "controller"=>"images", "action"=>"create"} permitted: false>

在 ImagesController中,这是 image_params 的定义方式。这.require(:image)是我认为在发送时丢失的首字母:

def image_params
  params.require(:image).
    permit(...)
end
4

1 回答 1

0

知道了!

有一个错字fieldName

我以前有它images[file],它应该是image[file]

于 2020-03-28T17:50:23.657 回答