ruby-on-rails - Rails 3 和嵌套 jQuery 文件上传模型

标签 ruby-on-rails ruby-on-rails-3 jquery file-upload

有人有关于使用嵌套属性让 jQuery 文件上传插件与 Rails 一起使用的建议/示例吗?

我的模型“has_many”附件并接受必要的嵌套属性。我想让它与 jQuery 文件上传一起使用,但一直无法找到任何好的示例来帮助我开始。

有人取得了这样的成就并能够提供一些指导吗?

谢谢。

最佳答案

我已成功将其设置为在编辑带有嵌套附件的模型时工作。
创建新模型时,这是不可能的 - 至少目前由于回形针的原因不可能。

你必须设置一些我发现的 jQuery 文件上传选项 here .

您应该在整个表单上调用fileUploadUI(),并将文件输入包装元素设置为dropZone。您还应该相应地设置 urlfieldNameformData

这是我的 JS 的样子(简化):

var $file_upload_form = $("form")
var attachable_id     = $file_upload_form.attr("id").match(/_(\d*)$/)[1]
var attachable_type   = $file_upload_form.attr("id").match(/edit_(\w*)_\d*$/)[1]

$($file_upload_form).fileUploadUI({
    url         : '/admin/attachments',
    fieldName   : "attachment[data]",
    formData    : [
        {
            name  : 'attachment[attachable_id]',
            value : attachable_id
        },
        {
            name  : 'attachment[attachable_type]',
            value : attachable_type
        }
    ],
    dropZone        : $('#attachments_dropzone'),
    uploadTable     : $('#upload_files'),
    downloadTable   : $('#download_files'),
    buildUploadRow  : function (files, index) {
        var file = files[index];
        return $('<tr><td>' + file.name + '<\/td>' +
                        '<td class="file_upload_progress"><div><\/div><\/td>' +
                        '<td class="file_upload_cancel">' +
                        '<button class="ui-state-default ui-corner-all" title="Cancel">' +
                        '<span class="ui-icon ui-icon-cancel">Cancel<\/span>' +
                        '<\/button><\/td><\/tr>');
    },
    buildDownloadRow: function (file) {
        return $('<tr><td><img alt="Photo" width="40" height="40" src="' + file.pic_path + '">' + file.name + '<\/td><\/tr>');
    }
})

关于ruby-on-rails - Rails 3 和嵌套 jQuery 文件上传模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5533879/

相关文章:

ruby-on-rails - 设备确认后自动登录

ruby-on-rails - 使用带有 Resque 后台作业的 Controller 方法

ruby-on-rails - 使用 postgres 在 rails 中保存日期和数据

jQuery - 选项卡,使用 url 加载 ajax 内容

jQuery fadeOut CSS3 替换

ruby-on-rails - 在 Rails 3 中添加来自 URL 的图像作为附件

ruby-on-rails - rails : how to test if parent has grandchildren present in child model

ruby-on-rails-3 - Rescu 的日志记录问题

iphone - 从 iPhone 将图像上传到 Rails 服务器(使用 Dragonfly)

javascript - 使用 Ajax 渲染部分集合