带有rails的Pandastream视频上传

时间:2014-02-01 21:23:56

标签: ruby-on-rails pandastream

我正在使用pandastream在我的应用程序中获取视频上传,但我无法绕过这个不在rails 4中的attr_accessible rails 3。 我知道它与强大的参数有关,但至于传递给代码的内容让我感到困惑,谢谢,这里有一些片段。

这是我的视频模型。

    class Video < ActiveRecord::Base
  validates_presence_of :panda_video_id


def panda_video
    @panda_video ||= Panda::Video.find(panda_video_id)
  end
end

和视频控制器。

class VideosController < ApplicationController
  def show
    @video = Video.find(params[:id])
    @original_video = @video.panda_video
    @h264_encoding = @original_video.encodings["h264"]
  end

  def new
    @video = Video.new(video_params)
  end

  def create
    @video = Video.create!(params[:video])
    redirect_to :action => :show, :id => @video.id 
  end

  def video_params

    params.require(:video).permit(:panda_video_id, :video)

  end
end

1 个答案:

答案 0 :(得分:2)

我有类似的问题。我重新构建了表单,以稍微不同的方式将信息传递给控制器​​。我能够按如下方式设置所有内容:

的视频控制器

class VideosController < ApplicationController
  def show
    @video = Video.find(params[:id])
    @original_video = @video.panda_video
    @h264_encoding = @original_video.encodings["h264"]
  end

  def new
    @video = Video.new
  end

  def create
    @video = Video.create!(video_params)
    redirect_to :action => :show, :id => @video.id
  end

  private
  def video_params
    params.permit(:title, :panda_video_id)
  end
end

视频/ new.html.erb

<%= form_for @video do |f| %>

    <!-- field where the video ID will be stored after the upload -->
    <input id="panda_video_id" type="hidden" name="panda_video_id"/>

    <label>Title</label>
    <input type="text" name='title' placeholder="Give a title">

    <!-- upload progress bar (optional) -->
    <div class='progress'><span id="progress-bar" class='bar'></span></div>

    <!-- file selector -->
    <div id="browse">Choose file</div>

<% end %>

<script>
    var upl = panda.uploader.init({
        'buttonId': 'browse',
        'progressBarId': 'progress-bar',
        'onQueue': function(files) {
            $.each(files, function(i, file) {
                upl.setPayload(file, {'csrf': "<%= form_authenticity_token %>"});
            })
        },
        'onSuccess': function(file, data) {
            $("#panda_video_id").val(data.id)
        },
        'onComplete': function(){
            $("#new_video").submit();
        }
    });
</script>