2个错误禁止保存此帖子。回形针ffmpeg

时间:2014-08-07 18:28:42

标签: ruby-on-rails ruby ruby-on-rails-4 attributes paperclip

2个错误禁止保存此帖子:

- 视频内容类型无效 - 视频无效

我尝试使用Paperclip Gem在dev中本地上传视频到我的网站但是,我得到一个带有播放按钮但不播放视频的黑屏,或者我在上传视频时收到上述错误消息。我正在使用Rails 4.0。

有人可以指导我如何摆脱这个错误,并使用Paperclip Gem成功上传视频吗?

模型

    class Post < ActiveRecord::Base

    belongs_to :user

    has_attached_file :image, :styles => { :medium => "300x300>", :thumb => "100x100>" }

    has_attached_file :video, :styles => {:mp4 => { :geometry => "300x300", :format => 'mp4' },:thumb => { :geometry => "150x150>", :format => 'jpg', :time => 5 }
        }, :processors => [:ffmpeg]



    validates_attachment_content_type :image, :content_type => ["image/jpg", "image/jpeg", "image/png", "image/gif"]
    validates_attachment_content_type :video, :content_type => ["video/mp4", "video/mov", "video/mpeg","video/mpeg4", "video/AVI"]

    validates :description, presence: true
    validates :image, presence: false
    validates :video, presence: false

end

Show.html

<div class="row">
    <div class="col-md-offset-4 col-med-8">
        <div class="panel panel-default">
        <div class="panel-heading center">
            <%= image_tag @post.image.url(:medium) %>
            <%= video_tag @post.video.url(:medium), controls: true, type: "video/MOV" %>
        </div>
        <div class="panel-body">
        <p><%= @post.description %></p>
        <p><strong><%= @post.user.name if @post.user %></strong></p>

    <% if @post.user == current_user %>
        <%= link_to edit_post_path(@post) do %>
        <span class="glyphicon glyphicon-edit"></span>
        Edit
      <% end %>
    <% end %>
    <%= link_to 'Back', posts_path %>
    </div>
</div>

移植

class AddAttachmentVideoToPosts < ActiveRecord::Migration
  def self.up
     change_table :posts do |t|
      t.attachment :video
    end
  end

  def self.down
    drop_attached_file :posts, :video
  end
end

后置控制器

def post_params
   params.require(:post).permit(:description, :image, :video)
end

配置/ enviroments的/ dev

scout::Application.configure do

  Paperclip.options[:command_path] = "/usr/local/bin"

以下是关于此other question的{{3}}链接。提前谢谢。

1 个答案:

答案 0 :(得分:0)

我无法发表评论所以我将在此处添加我的发现,希望它会有所帮助:

我注意到您验证了其内容类型的图片属性,并验证了图片的视频类型。

validates_attachment_content_type :image, :content_type => ["video/mp4", "video.MOV", "video/mpeg","video/mpeg4", "image/jpg", "image/jpeg", "image/png", "image/gif"] 

但是您没有验证视频属性的内容类型,视频也应该针对其内容类型进行验证,否则它将无效,图像和视频的正确内容类型验证应如下所示:< / p>

validates_attachment_content_type :image, :content_type => ["image/jpg", "image/jpeg", "image/png", "image/gif"]
validates_attachment_content_type :video, :content_type => ["video/mp4", "video.MOV", "video/mpeg","video/mpeg4"]