0

我正在使用带有 Paperclip 的 Rails 3.1,并尝试在单个页面上实现 Uploadify 以实现多个文件上传。我尝试了各种示例,包括:Rails3-Paperclip-Uploadify

目前我有一个 Upload 模型,它与我的 UploadImage 模型有一对多的关系 - 这是我到目前为止为我的视图设置的:

UploadImages/new.html.erb

<%= javascript_include_tag "http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js" %>
<%= javascript_include_tag "swfobject.js", "jquery.uploadify.v2.1.0.js" %>

<script type="text/javascript" charset="utf-8">
    <%- session_key = Rails.application.config.session_options[:key] -%> 
    $(document).ready(function() 
    {
        // Create an empty object to store our custom script data
        var uploadify_script_data = {};

        // Fetch the CSRF meta tag data
        var csrf_token = $('meta[name=csrf-token]').attr('content');
        var csrf_param = $('meta[name=csrf-param]').attr('content');

        // Now associate the data in the config, encoding the data safely
        uploadify_script_data[csrf_token] = encodeURI(encodeURI(csrf_param));

        $('.uploadify').uploadify
        ({
            uploader        :   '/uploadify/uploadify.swf',
            cancelImg       :   '/uploadify/cancel.png',
            multi           :   true,
            auto            :   false,
            onComplete      :   function(event, queueID, fileObj, response, data)
                                { 
                                    var dat = eval('(' + response + ')');
                                    $.getScript(dat.upload);
                                },
            scriptData      :   {
                                    '_http_accept': 'application/javascript',
                                    'format' : 'json',
                                    '_method': 'post',
                                    '<%= session_key %>' : encodeURIComponent('<%= u cookies[session_key] %>'),
                                    'authenticity_token': encodeURIComponent('<%= u form_authenticity_token %>'),
                                    'upload_id' : '<%= @upload.id %>'
                                }
        });         
    }); 


</script>

<%= form_for @upload.upload_images.build, :html => { :class => "upload", :multipart => true } do |f| %>  
                    <%= f.file_field :image, :class => "uploadify" %>
                    <%= submit_tag "Submit Upload", :disable_with => "Uploading", :class => "submit"%>
<% end %>

我看到了 uploadify flash 按钮,我选择了一些文件并提交,但这就是我在参数中看到的全部内容:

{"utf8"=>"✓", "authenticity_token"=>"K1bSH/FO0Hjdum0aiNU45mHJXezXTiCgh9XVmk1jrZM=", "commit"=>"提交上传", "action"=>"create", "controller"=>"upload_images"}

如您所见,没有发送文件数据,甚至我指定的 scriptData 也没有发送,我注意到其他人的代码使用脚本参数作为 uploadify 函数。在 PHP 框架中,他们将其指向处理保存的 .php 文件。我正在使用回形针,所以我不确定如何实现这个......也许这是我的问题?如果您需要任何其他详细信息,请告诉我。

4

1 回答 1

0

我通过查看这个问题找到了答案:uploadify rails 3 none after the file is selected

我必须将一些事件附加到我的提交按钮,并将脚本参数添加到uploadify 函数中,正如我所猜测的那样。

因此,对于我的 config/routes.rb 中的初学者,我需要添加:

post "upload_images/create"

在我看来,我补充说:

$('#submit').click(function(event){
            event.preventDefault();
        });


$('#submit').click(function(event){
            event.preventDefault();
            $('.uploadify').uploadifyUpload();
});

最后我将脚本参数添加到我最初的 uploadify 函数调用中:

script          :   '/upload_images/create'

经过几个小时的打击我的头终于开始工作并且有意义了!希望这对其他人有帮助。

于 2012-06-29T17:11:41.123 回答