Rails 5.2.1
ruby 2.5.1p57(2018-03-29修订版63029)[x86_64-linux]
rvm 1.29.4
我正在使用Active Admin表单创建一个新对象 - product
可以有很多support_docs
。 support_doc
有两个属性:
当我不包括support_doc :filename
输入时,表单工作正常 - 例如我可以附加文件没问题。但是当我包含filename
属性输入或任何其他输入字段时,文件输入字段消失(甚至在HTML DOM中)。
重现步骤:
has_many
部分中为B设置文件字段和文件名输入# == 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
# == 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
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行):
如您所见,文件输入字段被我包含的任何输入字段替换。我已经做了尽可能多的研究,但我找不到有类似问题的人!
doc.file_field
造成了这个问题。我用doc.input :doc_file as: :file
换了它。很明显,你必须无法将file_field
与inputs
绑定成嵌套形式!