I have a file model in my application that basically is a model with only the paperclip file in it. I’m trying to upload some files using some nested forms, but everytime I save the file model, the actual file gets uploaded, but all the paperclip default columns remains nil. Here is the model I’m working with:
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
This is the params of the (multi level) nested form I’m building the Arquivo with:
{"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>>}
This is what happens when I’m debugging in 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
I looked and looked hoping to find that this was caused by something stupid, but I simply can’t understand how this can be happening, especially the part where the Arquivo object is actually being validated but all the fields remain empty?!?!
I found the problem, and it actually had little to do with Paperclip:
It turns out that my Arquivo model which has the paperclip attachment didn’t have the paperclip attributes tagged as “attr_accessible”. In Rails 3.2 you have a white list of attributes, and if an attribute isn’t on the white list (attr_accessible), you can’t assign it using a form (need more info on that). This is used to protect a user manually adding a field to a form and submitting more data than he should.
Normally, Rails would throw a “Cannot mass-assign attributes: attr1, attr2, …” exception, but in this case it didn’t. I still don’t know if it was because of paperclip ignoring this error or because of the nesting level or both. Just adding the paperclip attributes to the white list worked.