我的应用程序中有一个文件模型,它基本上是一个只有回形针文件的模型。我正在尝试使用一些嵌套表单上传一些文件,但每次我保存文件模型时,实际文件都会被上传,但所有回形针默认列仍然为零。这是我正在使用的模型:
class Arquivo < ActiveRecord::Base
attr_accessible :arquivo, :arquivo_file_name, :arquivo_file_size, :arquivo_content_type, :arquivo_updated_at
has_one :aluno_documento
attr_accessor :arquivo_file_name, :arquivo_file_size, :arquivo_content_type, :arquivo_updated_at
has_attached_file :arquivo
validates :arquivo_file_name, :presence => true
validates_attachment_presence :arquivo
validates_attachment_size :arquivo, :less_than => 1.megabyte, :if => :arquivo
validates_attachment_content_type :arquivo, :content_type => 'application/pdf', :message => :invalid, :if => :arquivo
end
这是我正在构建 Arquivo 的(多级)嵌套表单的参数:
{"arquivo"=>#<ActionDispatch::Http::UploadedFile:0x00000009176958 @original_filename="SGCAv3.7.pdf", @content_type="application/pdf", @headers="Content-Disposition: form-data; name=\"pessoa[alunos_attributes][0][aluno_documentos_attributes][0][arquivo_attributes][arquivo]\"; filename=\"SGCAv3.7.pdf\"\r\nContent-Type: application/pdf\r\n", @tempfile=#<File:/tmp/RackMultipart20121123-16651-se21y5>>}
这就是我在 IRB 中调试时发生的情况:
ar = Arquivo.new(passing params above)
=> #<Arquivo id: nil, arquivo_file_name: nil, arquivo_content_type: nil, arquivo_file_size: nil, arquivo_updated_at: nil>
ar.arquivo_file_name
=> "SGCAv3.7.pdf"
ar.arquivo_content_type
=> "application/pdf"
ar.save
=> true
ae = Arquivo.last
=> #<Arquivo id: 9, arquivo_file_name: nil, arquivo_content_type: nil, arquivo_file_size: nil, arquivo_updated_at: nil>
ae.valid?
=> false
ae.arquivo_file_name
=> nil
我看了又看,希望发现这是由一些愚蠢的事情引起的,但我根本无法理解这是怎么发生的,尤其是 Arquivo 对象实际上正在被验证但所有字段仍然为空的部分?!?!