2014-10-01 76 views
2

這是我在stackoverflow上的第一篇文章。我正在開發一款軌道應用程序,它需要一個歌曲模型,其中有一個通過回形針附加的.ogg音頻文件。由於某些瀏覽器音頻格式限制,我使用.ogg格式。Rails回形針附件驗證失敗.ogg格式

不幸的是,當創建一個新的歌曲對象時,它似乎無法通過所有的格式驗證,導致無法上傳音頻文件。

我試過了幾種格式的描述,比如'audio/ogg','audio,oga',video/ogg'...但似乎沒有任何效果。上傳.mp3文件可以正常工作,但由於上面解釋的原因,我需要使用.ogg。

我使用回形針上傳應用程序的其他模型中的圖像文件,它工作正常,所以它似乎我失去了一些東西......在此先感謝您的幫助!

模型,song.rb:

class Song < ActiveRecord::Base 
    attr_accessible :name, :lyrics, :track_order, :music_file, :url 

    has_attached_file :music_file, dependent: :destroy 

    validates_presence_of :name, :lyrics, :track_order, :record_id 
    validates_attachment_presence :music_file 
    validates_attachment_content_type :music_file, :content_type => ['audio/ogg', 'video/ogg'] 

    belongs_to :record 

    default_scope order('track_order ASC') 

    before_validation :set_file_url 

    private 

    def set_file_url 
    self.url = music_file.url 
    end 

end 

的錯誤,我得到:

ArgumentError (uncaught throw #<ActiveModel::Errors:0x0000000560df60 @base=#<Song id: 12, name: "Las Horas", lyrics: "Letra aquí", track_order: 1, record_id: 2, created_at: "2014-09-30 14:06:05", updated_at: "2014-09-30 15:27:25", music_file_file_name: "lshoras.ogg", music_file_content_type: "video/ogg", music_file_file_size: 3220214, music_file_updated_at: "2014-10-01 08:59:56", url: "/system/songs/music_files/000/000/012/original/lsho...">, @messages={:music_file=>["has an extension that does not match its contents"], :name=>[], :lyrics=>[], :track_order=>[]}>): 
    app/controllers/songs_controller.rb:33:in `throw' 
    app/controllers/songs_controller.rb:33:in `update' 

回答

1

看來你的OGG文件有一個錯誤的啞劇,檢查this,你可以看到該ogg文件應該有audio/ogg,但你的似乎有video/ogg(ogv)。你可以用一下:

file -b --mime lshoras.ogg 

如果這沒有解決您的問題,您可以檢查this禁用或修改驗證:

Paperclip.options[:content_type_mappings] = { ogg: 'application/ogg' } 
+0

檢查的結果是: '應用/ OGG; charset = binary' 嘗試添加 ':content_type => ['application/ogg','audio/ogg','video/ogg']'但是一直失敗。 也嘗試從其他來源上傳不同的ogg文件,但驗證失敗。 – 2014-10-01 10:18:37

+0

做什麼'Paperclip.options [:content_type_mappings] = {ogg:'application/ogg'}'? – 2014-10-01 12:46:20

+0

非常感謝,最後一個選項奏效! – 2014-10-01 13:21:01