1

我正在将 ruby​​ 1.8.7 上的 rails 3.0 应用程序迁移到 1.92 上的 3.2 应用程序。所有的测试都通过了,只有一个。下面有很多文字,但问题并不复杂。我只是有点啰嗦。

我有一个电子邮件模型,其中包含文件,用 Paperclip 处理。我有一个在 3.0 / 1.8.7 中工作的创建测试,它使用本地文件为上传文件发送嵌套属性。对于测试,我使用了 Gemfile,因为它很小并且总是在我的仓库中。

测试是这样写的:

creatable_attributes = creatable_resource.attributes.merge( {"uploads_attributes"=>[{"name"=>"", "member_id"=>"764", "file"=>File.new('Gemfile')}]} )

post :create, post_var => creatable_attributes 

我现在将向您展示 3 件事的调试信息,针对两个repo 分支(3.0/1.8.7 和 3.2/1.92)。您会看到导致嵌套上传无法保存的关键区别,我不知道如何修复它。

1) 在测试中看到的 BEFORE SENDING 发送的数据 2) 在接收控制器中看到的数据 3) 在控制器中创建的相应上传

* 3.0/1.8.7 -- “工作”版本 *

1)测试中发送的数据如 BEFORE SENDING 所见

{
    "uploads_attributes" => [
        [0] {
                 "file" => #<File:Gemfile> -rw-r--r--  1 benlieb  staff  2506 Jan 26 13:15 Gemfile,
            "member_id" => "764",
                 "name" => ""
        }
    ],
            "created_at" => nil,
               "subject" => "string_28636",
         "markup_filter" => nil,
             "member_id" => 6891662,
                  "body" => "string_29201",
            "updated_at" => nil
}

2)在接收控制器中看到的参数

{
    "controller" => "emails",
         "email" => {
        "uploads_attributes" => [
            [0] {
                     "file" => #<File:Gemfile>   -rw-r--r--  1 benlieb  staff  2506 Jan 26 13:15 Gemfile,
                "member_id" => "764",
                     "name" => ""
            }
        ],
                "created_at" => nil,
                   "subject" => "string_28636",
                 "member_id" => 6891662,
             "markup_filter" => nil,
                "updated_at" => nil,
                      "body" => "string_29201"
    },
        "action" => "create"
}

3)在控制器中创建的相应上传(这工作并保存)

[
    [0] #<Upload:0x104a9b168> {
                       :id => nil,
                     :name => "",
                :member_id => 764,
           :file_file_name => "Gemfile",
        :file_content_type => "text/plain",
           :file_file_size => 2506,
          :file_updated_at => Sat, 26 Jan 2013 13:16:51 EST -05:00,
               :created_at => nil,
               :updated_at => nil
    }
]

* 3.2/1.9.2 -- '失败' 版本 *

1)发送的数据如BEFORE SENDING所示,在测试中(注意文件与3.0/1.9.2相同)

{
                    "id" => nil,
               "subject" => "string_3556259",
                  "body" => "string_35571",
             "member_id" => 1644867,
         "markup_filter" => nil,
            "created_at" => nil,
            "updated_at" => nil,
    "uploads_attributes" => [
        [0] {
                 "name" => "",
            "member_id" => "764",
                 "file" => #<File:Gemfile>   -rw-r--r--  1 benlieb  staff  2828 Jan 26 13:34 Gemfile
        }
    ]
}

2)接收控制器中看到的参数(注意文件数据已更改)

{
         "email" => {
                        "id" => nil,
                   "subject" => "string_3556259",
                      "body" => "string_35571",
                 "member_id" => "1644867",
             "markup_filter" => nil,
                "created_at" => nil,
                "updated_at" => nil,
        "uploads_attributes" => [
            [0] {
                     "name" => "",
                "member_id" => "764",
                     "file" => "#<File:0x0000010343d1f0>"
            }
        ]
    },
    "controller" => "emails",
        "action" => "create"
}

3)控制器中创建的对应上传(该文件保存失败,出错了)

[
    [0] #<Upload:0x000001061aeee0> {
                       :id => nil,
                     :name => "",
                :member_id => 764,
           :file_file_name => nil,
        :file_content_type => nil,
           :file_file_size => nil,
          :file_updated_at => nil,
               :created_at => nil,
               :updated_at => nil
    }
]

这里有一些文件可以很好地衡量:https ://gist.github.com/4643779

所以我的问题是发生了什么,我该如何解决。

4

1 回答 1

0

找到了解决方案。我不得不将 File.new 更改为 Rack::Test::UploadedFile.new

于 2013-01-30T16:59:03.507 回答