不允许的参数:上传视频时的视频[回形针]

时间:2019-02-01 01:49:43

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

我今天得到Unpermitted parameter: video,我的模特:

class Video < ActiveRecord::Base
  # Extends

  # Includes

  # Associations
  belongs_to :videoable, polymorphic: true

  has_attached_file :video
  validates_attachment_content_type :video, content_type: /\Avideo\/.*\Z/
  validates_attachment_file_name :video, :matches => [/3gp\Z/, /mp4\Z/, /flv\Z/]
end
class Campaign < ActiveRecord::Base
   has_many :videos, as: :videoable

  # accepts_nested_attributes
  accepts_nested_attributes_for :videos
end

控制器:

params.require(:campaign).permit(videos_attributes: [:video, :video_file_name, :id, :_destroy])

视图:

= f.simple_fields_for :videos, Video.new do |v|
       = v.file_field :video, multiple: true
= f.simple_fields_for :videos do |v|
       = video_tag(v.object.video.url, controls: true, size: "148x148") if v.object.video?
       br
       .form-inline
          = v.check_box :_destroy, class: 'form-control'
              | Delete

不确定在这种情况下我错过了什么,我尝试用Google搜索它,但结果与我的设置相同,感谢您的所有建议,谢谢 我正在使用回形针来支持文件上传

参数请求:

"videos_attributes"=>{"0"=>{"video"=>[#<ActionDispatch::Http::UploadedFile:0x007f5bcfad6f88 @tempfile=#<Tempfile:/tmp/RackMultipart20190201-6213-waz9el.mp4>, @original_filename="facebook_user_link.mp4", @content_type="video/mp4", @headers="Content-Disposition: form-data; name=\"campaign[videos_attributes][0][video][]\"; filename=\"facebook_user_link.mp4\"\r\nContent-Type: video/mp4\r\n">], "_destroy"=>"0"}}

1 个答案:

答案 0 :(得分:0)

最后,我在设置的视图中找到了问题: = v.file_field :video, multiple: true
所以我需要像这样设置视频的参数:
params.require(:campaign).permit(videos_attributes: [:video_file_name, :id, :_destroy, video: []])
那么上传过程将正常进行。