【问题标题】:Rails :Can't save audio file (paperclip)Rails:无法保存音频文件(回形针)
【发布时间】:2018-04-29 17:44:11
【问题描述】:

我无法保存音频文件,你能告诉我如何保存音频文件吗? 我用宝石回形针!

日志是 命令 :: file -b --mime '/var/folders/1z/psyvq9q911b7hclx5xw03dk40000gn/T/4266fc074db087b967597da8e4d2d11020171116-3739-1bal5v0.mp3' [回形针] 内容类型欺骗:文件名 as333auda.mp3(来自 Headers 的音频/mp3,来自 Extension 的 ["audio/mpeg"]),从文件命令发现的内容类型:video/mp4。请参阅文档以允许这种组合。 (0.1ms) 开始交易 命令 :: file -b --mime '/var/folders/1z/psyvq9q911b7hclx5xw03dk40000gn/T/4266fc074db087b967597da8e4d2d11020171116-3739-wh10af.mp3' [回形针] 内容类型欺骗:文件名 as333auda.mp3(来自 Headers 的音频/mp3,来自 Extension 的 ["audio/mpeg"]),从文件命令发现的内容类型:video/mp4。请参阅文档以允许这种组合。 (0.2ms) 回滚事务 重定向到http://localhost:3000/listings/new 完成 302 Found in 128ms (ActiveRecord: 3.0ms)

向您展示模型listing.rb

class Listing < ApplicationRecord
     belongs_to :user
     has_many :photos
     has_many :likes
     has_many :customers
     has_many :reviews
     validates :listing_title, presence: true

    has_attached_file  :file,  :storage => :s3,
                               :s3_credentials => "#{Rails.root}/config/s3.yml"

   validates_attachment :file, content_type: { content_type: ['file/mp3'] }


 end

向您展示 new.html.erb

<%= form_for @listing do |f| %>
    <div class="row">
     <div class="col-md-12 select">
       <div class="form-group">
        <label>タイトル</label>
           <%= f.text_field :listing_title, autofocus: true, :placeholder => "タイトル", :class => 'form-control', required: "true" %>
       </div>
      </div>
     </div>
     <br>

     <div class="row">
      <div class="col-md-12 select">
       <div class="form-group">
        <label>ファイル</label>
          <%= f.file_field :file, :class => 'form-control' %>
       </div>
      </div>
     </div>
     <br>

     <div class="actions">
       <%= f.submit "Save", class: "btn btn-danger" %>
     </div>
<% end %>

控制器没问题!我设置了参数! 我需要什么才能成功保存音频文件? 请告诉我!

【问题讨论】:

  • 不是MP3文件的mime类型audio/mpeg吗? 我无法保存 不是很有帮助。有错误信息吗?验证错误?日志文件中是否有消息。如果你尝试会发生什么?
  • 对不起。我编辑了!添加问题内容。

标签: ruby paperclip ruby-on-rails-5


【解决方案1】:

MP3 音频文件已 mime 键入 "audio/mpeg"

将您的内容类型更改为"audio/mpeg"

【讨论】:

  • 谢谢你! validates_attachment :file, content_type: { content_type: [ "audio/mpeg" ] } 对吗?
  • 但我无法保存!
  • 尝试像l = Listing.create file: File.new('/path/anymp3.mp3')一样在rails console中创建Listing并显示l.errors.full_messages的输出
【解决方案2】:

尝试:

{ content_type: ['audio/mp3'] }

如果还是不行,可以试试这个:

{ content_type: ['audio/mpeg', 'audio/x-mpeg', 'audio/mp3', 'audio/x-mp3', 'audio/mpeg3', 'audio/x-mpeg3', 'audio/mpg', 'audio/x-mpg', 'audio/x-mpegaudio'] }

【讨论】:

  • 谢谢你!我明白了,我被你解决了!谢谢!!
  • @DaichiDaichi 不客气 :) 不久前我也在为同样的事情苦苦挣扎。
猜你喜欢
  • 2010-12-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多