2017-04-15 102 views
0

非常感谢您的帮助。我试了几天,我无法弄清楚。我正在尝试构建视频上传表单。令我惊讶的是,我无法找到关于这个话题的很多信息。Rails视频上传回形针回形针-av-transcoder表格保存

我正在用paperclip-av-transcoder使用回形针。它似乎让我创建一个Video.new记录,但我不能得到它以节省@user.videos.build(video_params)。我知道这些参数正在通过,因为它们显示在以前的错误消息中。我不明白的错误,只是“没有。没有工作”的字符串我把。

class VideosController < ApplicationController 

    def new 
    @user = User.find(current_user) 
    @video = @user.build_video 
    end 

    def create 
    @user = User.find(current_user) 
    @video = @user.videos.build(video_params) 
    if @video.save 
     redirect_to admin_ad_pg_path, :flash => { :error => "It worked!" } 
    else 
     redirect_to admin_ad_pg_path, :flash => { :error => "Nope. Didn't work." } 
    end 
    end 

    private 
    def video_params 
     params.require(:video).permit(:avatar) 
    end 
end 


Video Model: 

class Video < ApplicationRecord 
    belongs_to :user 
    has_attached_file :avatar, :styles => { 
    :medium => { :geometry => "640x480", :format => 'flv' }, 
    :thumb => { :geometry => "100x100#", :format => 'jpg', :time => 10 } 
    }, :processors => [:transcoder] 
    validates_attachment :avatar, content_type: { content_type: /\flv\/.*\Z/ } 
end 

User Model 

class User < ApplicationRecord 
has_many :videos 
end 



Video Form: 
<%= flash[:error] %> 
     <%= form_for @video, url: user_videos_path(current_user), :html => { multipart: true } do |f| %> 
     <div class="form-group"> 
      <%= f.label :avatar %> 
      <%= f.file_field :avatar, class: 'form-control' %> 
     </div> 
     <%= f.submit 'Submit',class: 'btn btn-default' %> 
     <% end %> 

Paperclip Migration: 
class AddAttachmentAvatarToVideos < ActiveRecord::Migration 
    def self.up 
    change_table :videos do |t| 
     t.attachment :avatar 
    end 
    end 

    def self.down 
    remove_attachment :videos, :avatar 
    end 
end 

谢谢, 马特

+0

难道我只是完全错误的做法,这就是为什么没有答案? –

回答

0

嘿你做了这个迁移如果不是请做到这一点,再试一次。

def change 
    add_column :users, :avatar_meta, :data_type 
end 

可以使用的数据类型:JSON,:jsonb,:hstore甚至只是:字符串