2017-04-15 79 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甚至只是:字符串