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

时间:2018-09-13 08:35:43

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

如何停止将文件输入字段替换为文本输入字段?

版本:

  

Rails 5.2.1

     

红宝石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. 创建一个具有许多其他模型(B)的模型(我们称其为A)
  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

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

1 个答案:

答案 0 :(得分:1)

已修复!

doc.file_field引起了问题。我使用doc.input :doc_file as: :file进行了切换。显然,您一定不能将file_fieldinputs绑定成嵌套形式!