3

これは、stackoverflow に関する私の最初の投稿です。クリップで添付された .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'
4

1 に答える 1

2

あなたの ogg ファイルは間違った MIMEを持っaudio/oggいるようですvideo/ogg。次の方法で確認できます。

file -b --mime lshoras.ogg

これで問題が解決しない場合は、これをチェックして検証を無効にするか変更できます。

Paperclip.options[:content_type_mappings] = { ogg: 'application/ogg' } 
于 2014-10-01T09:55:33.990 に答える