【问题标题】:Rails 5.2.1: File input being replaced by text inputs - ActiveAdmin nested attributes formRails 5.2.1:文件输入被文本输入替换 - ActiveAdmin 嵌套属性表单
【发布时间】:2018-09-13 08:35:42
【问题描述】:

如何阻止我的文件输入字段被我的文本输入字段替换?

版本:

Rails 5.2.1

ruby 2.5.1p57(2018-03-29 修订版 63029)[x86_64-linux]

rvm 1.29.4

我正在使用 Active Admin 表单创建一个新对象 - product,它可以有多个 support_docssupport_doc 有两个属性:

  1. Active Storage 文件附件
  2. 文件名

当我不包含 support_doc :filename 输入时,表单可以正常工作 - 例如,我可以附加文件没问题。但是,当我包含 filename 属性输入或任何其他输入字段时,文件输入字段就会消失(甚至在 HTML DOM 中也不存在)。

重现步骤:

  1. 创建一个包含许多其他模型 (B) 的模型(我们称之为 A)
  2. 在 A 中,允许 B 的嵌套属性
  3. 在创建 A 的表单中,在嵌套属性 has_many 部分中为 B 设置文件字段和文件名输入

产品.rb

# == Schema Information
#
# Table name: products
#
#  id         :integer          not null, primary key
#  title      :string
#  created_at :datetime         not null
#  updated_at :datetime         not null


class Product < ApplicationRecord
  has_many :support_docs, inverse_of: :product

  accepts_nested_attributes_for :support_docs
end

Support_doc.rb

# == Schema Information
#
# Table name: support_docs
#
#  id         :integer          not null, primary key
#  created_at :datetime         not null
#  updated_at :datetime         not null
#  filename   :string
#  product_id :integer


class SupportDoc < ApplicationRecord
  has_one_attached :doc_file
  belongs_to :product

  validates_presence_of :product
end

products.rb(Active Admin 资源中的表单)

ActiveAdmin.register Product do
  permit_params :title, support_docs_attributes: [:doc_file, :filename]

  form do |f|
    f.inputs do
      f.input :title
      f.has_many :support_docs do |doc|
        doc.file_field :doc_file, direct_upload: true
        doc.input :filename
      end
    end
    f.actions
  end
end

示例:

当我不包含 :filename 输入时(products.rb 中的第 9 行):

当我包含 :filename 输入时:

如您所见,文件输入字段被我包含的任何输入字段替换。我已经对此进行了尽可能多的研究,但找不到任何有类似问题的人!

【问题讨论】:

  • @sawa 抱歉,更新了我的问题以包含问题

标签: ruby-on-rails ruby-on-rails-5 activeadmin rails-activestorage


【解决方案1】:

已修复!

doc.file_field 导致了问题。我用doc.input :doc_file as: :file 切换它。显然,您不能将file_field 绑定到带有inputs 的嵌套表单中!

【讨论】:

    猜你喜欢
    • 2022-10-01
    • 2015-08-25
    • 1970-01-01
    • 1970-01-01
    • 2011-08-02
    • 2023-04-01
    • 1970-01-01
    • 2020-07-22
    • 1970-01-01
    相关资源
    最近更新 更多