【发布时间】: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_docs。 support_doc 有两个属性:
- Active Storage 文件附件
- 文件名
当我不包含 support_doc :filename 输入时,表单可以正常工作 - 例如,我可以附加文件没问题。但是,当我包含 filename 属性输入或任何其他输入字段时,文件输入字段就会消失(甚至在 HTML DOM 中也不存在)。
重现步骤:
- 创建一个包含许多其他模型 (B) 的模型(我们称之为 A)
- 在 A 中,允许 B 的嵌套属性
- 在创建 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 行):
如您所见,文件输入字段被我包含的任何输入字段替换。我已经对此进行了尽可能多的研究,但找不到任何有类似问题的人!
【问题讨论】:
-
@sawa 抱歉,更新了我的问题以包含问题
标签: ruby-on-rails ruby-on-rails-5 activeadmin rails-activestorage