Question

I am migrating a rails 3.0 app on ruby 1.8.7 to a 3.2 app on 1.92. All tests are passing but one. There is a lot of text below, but the problem is not complicated to see. I just had to be a little longwinded.

I have an Email model that has_many Uploads which are files, handled with Paperclip. I have a create test that was working in 3.0 / 1.8.7 that sent nested attributes for the upload file using a local file. For the test I used the Gemfile, as it is small and is always going to be in my repo.

The test is written like this:

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

post :create, post_var => creatable_attributes 

I'll now show you debugging info for 3 things, for BOTH repo branches (3.0/1.8.7 and 3.2/1.92.) You'll see a key difference which is causing the nested Upload to not save and I don't know how to fix it.

1) The data sent as seen BEFORE SENDING, in the test 2) The data as seen in the receiving controller 3) The corresponding Upload as created in the controller

*3.0/1.8.7 -- the 'working' version *

1) The data sent as seen BEFORE SENDING, in the test

{
    "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) The params as seen in the receiving controller

{
    "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) The corresponding Upload as created in the controller (This works and is saved)

[
    [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 -- the 'failing' version *

1) The data sent as seen BEFORE SENDING, in the test (notice the file is the same as 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) The params as seen in the receiving controller (notice the file data has changed)

{
         "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) The corresponding Upload as created in the controller (this file fails to be saved, something has gone wrong)

[
    [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
    }
]

And here's some files for good measure: https://gist.github.com/4643779

So my question is what's going on and how can I fix it.

Was it helpful?

Solution

Found the solution. I had to change File.new to Rack::Test::UploadedFile.new

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top