我有一个表单,提示输入作业和要附加的 job_files。当作业保存因验证失败时,选定的作业文件会在表单重新显示时消失。如何保留子字段?所有父字段都保留在表单中,但子字段已消失。
工作模式:
class Job < ActiveRecord::Base
has_many :job_files, :dependent => :destroy
accepts_nested_attributes_for :job_files, :allow_destroy => true
validates_acceptance_of :disclaimer, :allow_nil => false, :accept => true, :on => :create, :message=>'Must accept Terms of Service'
结尾
job_files 模型:
class JobFile < ActiveRecord::Base
belongs_to :job
has_attached_file :file
end
工作控制器:
def new
@upload_type = UploadType.find_by_id(params[:upload_job_id])
@job = Job.new
@job.startdate = Time.now
10.times {@job.job_files.build}
@global_settings = GlobalSettings.all
end
def create
@global_settings = GlobalSettings.all
if params[:cancel_button]
redirect_to root_path
else
@job = Job.new(params[:job])
@job.user_id = current_user.id
@upload_type = UploadType.find_by_id(@job.upload_type_id)
if @job.save
JobMailer.job_uploaded_notification(@upload_type,@job).deliver
flash[:notice] = "Job Created"
redirect_to root_path
else
# retain selected files if save fails
(10 - @job.job_files.size).times { @job.job_files.build }
flash[:error] = "Job Creation Failed"
render :action => :new
end
end
end
job_files 部分在工作表中:
<%= form.fields_for :job_files, :html=>{ :multipart => true } do |f| %>
<li class="files_to_upload">
<%= f.file_field :file %>
</li>
<% end %>
任何帮助将不胜感激,因为我在网上找不到任何解决方案。