2011-10-10 32 views
1

我只是想用uploadify和回形针上传一个文件。uploadify和回形针3.1.01

这是索引视图:

<%- 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 

现在,当我上载一个文件,没有引发错误,无论是客户端还是服务器端,但它看起来像回形针没有得到由uploadify发送给他的参数。事实上,如果我从模型中删除验证,则会创建不带参数的资源。

在控制器I添加:

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显示我只有标题!

回答

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')) 
    }