非常感谢你的帮助。我已经尝试了几天,但我无法弄明白。我正在尝试构建视频上传表单。令我惊讶的是,我找不到有关该主题的更多信息。
我正在使用Paperclip和paperclip-av-transcoder。这似乎是让我创建一个Video.new
记录,但我无法保存@user.videos.build(video_params)
。我知道params正在传递,因为它们显示在先前的错误消息中。我没有错误,只是" Nope。没有工作"我输入的字符串。
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甚至只是:string