Rails 5.2.1:文件输入被文本输入替换 - ActiveAdmin嵌套属性表

问题描述 投票:1回答:1

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

版本:

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. 活动存储文件附件
  2. 文件名

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

重现步骤:

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

Product.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行):without line 9

当我确实包括:filename输入:line 9 included

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

ruby-on-rails ruby-on-rails-5 activeadmin rails-activestorage
1个回答
1
投票

固定!

doc.file_field造成了这个问题。我用doc.input :doc_file as: :file换了它。很明显,你必须无法将file_fieldinputs绑定成嵌套形式!

© www.soinside.com 2019 - 2024. All rights reserved.