Reputation: 2876
I'm using Rails 4.0.0 with Paperclip 4.1.1 for attaching mp3 and pdf files. I'm writing my integration tests in Rspec with Capybara.
I have content type and file name validations in place for both types of files.
class Song < ActiveRecord::Base
validates :title, presence: true
validates :writeup, presence: true
has_attached_file :mp3
validates_attachment :mp3,
:content_type => { :content_type => "audio/mp3" },
:file_name => { :matches => [/mp3\Z/] }
has_attached_file :chords
validates_attachment :chords,
:content_type => { :content_type => 'application/pdf' },
:file_name => { :matches => [/pdf\Z/] }
end
I use this in my integration test to fill in attributes for a valid song:
def fill_in_valid_song
fill_in("Title", with: "Valid Song")
fill_in("Writeup", with: "Description of song")
attach_file("Mp3", File.join(Rails.root, "/spec/factories/Amazing_Grace.mp3" ))
attach_file("Chords", File.join(Rails.root, "/spec/factories/FakeChordChart.pdf" ))
end
When I run the integration test for creating a valid song, the pdf file is accepted, but the mp3 file fails the content type validation.
When I follow the same steps myself in the browser, the song uploads successfully without errors. The model spec also passes using the same file.
I thought the problem might be the capital "M" in "Mp3" when I attach the file, but this is just to specify which file field the attachment goes with. When I tried changing it to a lowercase "m", the error changed to Capybara not being able to find the field.
Upvotes: 3
Views: 2309
Reputation:
Change the content type validation for the mp3 file to be:
validates_attachment :mp3,
:content_type => { :content_type => ["audio/mpeg", "audio/mp3"] },
:file_name => { :matches => [/mp3\Z/] }
The RFC defined MIME type for mp3 files is audio/mpeg
. However some browsers load them as audio/mp3
which is probably why it works through the browser.
If this doesn't work you could also add audio/mpeg3
and audio/x-mpeg-3
as I've seen these used too.
Upvotes: 4