如何使用简单表格和直接上载ActiveStorage

当我尝试使用Simple Form gem上传视频时遇到麻烦。我为此使用activeStorage和本地存储。 我的表单如下:

= simple_form_for @film do |f|
  = f.error_notification
  = f.input :title,as: :string
  = f.input :description,as: :string
  = f.input :cover_img,as: :file

  = f.input :film_link,as: :file,direct_upload: true
  = f.button :submit
  = link_to 'back',:back,class: 'btn btn-secondary'

我已按照https://edgeguides.rubyonrails.org/active_storage_overview.html的指示进行操作 因此,我在应用程序中包含了js和CSS文件。但这行不通。通过simple_form传递direct_upload: true似乎有些麻烦。 我也找到了文章https://phase2online.com/blog/2018/10/03/easily-upload-files-with-active-storage-in-rails-5-2/,并从here提取了git repo

这将在您在_form上使用form_for时起作用。当我将表单更改为使用simple_form gem(而不是form_form使用simple_form_for)时,它将不起作用。

有人知道为什么这不起作用吗? 我使用ruby 2.6.3和Rails 5.2.3和simple_form(5.0.1)

x13575102949 回答:如何使用简单表格和直接上载ActiveStorage

我建议为此添加一个自定义输入,以避免在视图文件中重复。将其放在文件app/inputs/direct_upload_file_input.rb中:

# frozen_string_literal: true
class DirectUploadFileInput < SimpleForm::Inputs::FileInput
  def input_html_options
    super.merge({ direct_upload: true })
  end
end
,

有办法使这项工作可行。

  1. 我们可以按照hashrocket的建议将f.input更改为f.file_field,但是简单形式的验证将不起作用,也许我们必须在此输入中添加类以及之前的额外div。
  2. 我们可以在f.input中添加html属性。

对于我来说,这是有效的,我希望它也会对其他人有所帮助。

= simple_form_for @film do |f|
  = f.error_notification
  = f.input :title,as: :string
  = f.input :description,as: :string
  = f.input :cover_img,as: :file,input_html: { data: { direct_upload_url: '/rails/active_storage/direct_uploads' } }
  = f.input :film_link,input_html: { data: { direct_upload_url: '/rails/active_storage/direct_uploads' } }
  = f.button :submit
  = link_to 'back',:back,class: 'btn btn-secondary'
本文链接:https://www.f2er.com/3109462.html

大家都在问