在rails 3.1上的uploadify和paperclip

时间:2011-10-10 12:22:49

标签: paperclip ruby-on-rails-3.1 uploadify

我正在尝试使用uploadify和paperclip上传文件。

这是索引视图:

<%- session_key = Rails.application.config.session_options[:key] -%>

$('#upload').uploadify({
    'uploader'  : 'uploadify.swf',
    'script'    : '/videos',
    'cancelImg' : 'images/cancel.png',
    'buttonText'  : 'Add video!',
    'folder'    : '/public',
    'auto'      : true,
    'scriptData' : {"<%= key = Rails.application.config.session_options[:key] %>" : "<%= cookies[key] %>",
        "<%= request_forgery_protection_token %>" : "<%= form_authenticity_token %>",
    },
    onError : function (event, id, fileObj, errorObj) {
        alert("error: " + errorObj.info);
    }
});

这是我的模特:

class Video < ActiveRecord::Base

  has_attached_file :source

  validates_attachment_presence :source

end

...这是我的迁移文件:

class CreateVideos < ActiveRecord::Migration
 def self.up
  create_table :videos do |t|
   t.string :source_content_type
   t.string :source_file_name
   t.integer :source_file_size
   t.timestamps
 end
end

 def self.down
  drop_table :videos
 end
end

现在,当我上传文件时,没有提出错误,无论是客户端还是服务器端,但看起来像paperclip没有得到uploadify发送给他的参数。实际上,如果我从模型中删除验证,则会创建没有参数的资源。

在控制器中我添加:

def create
 logger.info(params.inspect) (1)
 @video = Video.new(params[:video])
 @video.save
 logger.info @video.inspect (2)
end

在development.log(1)给我

{"Filename"=>"prova.mov", "authenticity_token"=>"9q5w5LipGoBj Iac055OPbDofFU5FMbpEX675 RqOqs=", "folder"=>"/public", "_StreamVideo_Api_session"=>...

(2)未经验证

#<Video id: 1, source_content_type: nil, source_file_name: nil, source_file_size: nil, created_at: "2011-10-10 12:12:38", updated_at: "2011-10-10 12:12:38">

(2)验证

#<Video id: nil, source_content_type: nil, source_file_name: nil, source_file_size: nil, created_at: nil, updated_at: nil>

哪里可能有问题? 提前感谢您的回答!

在我看来,我说:

<h1>New video</h1>

<% form_for :video, :html => { :multipart => true } do |f| %>
  <%= f.file_field :source%>
<% end %>

http://localhost:3000/videos/new只向我展示了标题!!

1 个答案:

答案 0 :(得分:0)

代码看起来没问题,你应该只添加encodeURI函数。在Uploadify 2.1中 它应该与:

 'scriptData' : {
     $('meta[name=csrf-param]').attr('content') : encodeURI(encodeURIComponent($('meta[name=csrf-token]').attr('content')))
  }

使用Uploadify 3.1:

'formData': {
         $('meta[name=csrf-param]').attr('content') : encodeURI($('meta[name=csrf-token]').attr('content'))
    }