jejacks0n/mercury — Issue #96: Image upload issue - uploader.js expects a different data format than Rails returns
Author: cannikin
Created: 2011-11-17 20:25:23+00:00
State: open

Just installed mercury (ref: 4e4b9922705a) and every time I try to upload an image I was getting this error (pops up in an alert box):

Unable to process response: TypeError: Cannot read property 'url' of undefined

After some digging I found the line of Coffeescript that was trigging the error:

Mercury.trigger('action', {action: 'insertImage', value: {src: response.image.url}})

When you inspect the response from the server, the retuned JSON looks like this:

{"created_at":"2011-11-17T20:18:41Z","id":12,"image_content_type":"image/png","image_file_name":"trainer_buttons.png","image_file_size":12716,"image_updated_at":"2011-11-17T20:18:41Z","updated_at":"2011-11-17T20:18:41Z","url":"/system/images/12/original/trainer_buttons.png?1321561121"}

There is no `image` key wrapping that object and so `response.image.url` fails. One fix is to update the Coffeescript and just remove the reference to `image`:

Mercury.trigger('action', {action: 'insertImage', value: {src: response.url}})

The other would be to tell `mercury/images_controller.rb` to wrap the resulting `@image` that's returned in something like `{ :image => @image }` but then you have to go through the whole process of manually defining a `:location` header because `respond_with` expects to point back to the REST URL for the object that it's rendering, yadda yadda.

Am I crazy or should I submit a pull request with a fix?