Skip to content Skip to sidebar Skip to footer

Jquery File Upload Rails Redirect

I am using jquery-file-upload to upload files. After files are finished uploading, i want to redirect to the edit path of the uploaded document. I am using javascript to submit t

Solution 1:

there's a couple of things you have to consider. First, you have to be able to get the url to the edit action. If your js is in the page and not in an asset file, the following change should work.

jqXHR = data.submit()
  .complete(function() { window.location = "#{url here}" })

you also need to use <%= %> if you are using erb instead of #{...}. If this is in an asset file, you don't have access to instance variables so you have to rely on the response of the server. As an example, you can set the response to be json.

$('#fileupload').fileupload({
  dataType: 'json',
  add: function(json) {
   jqXHR = data.submit()
    .complete(function() { window.location = json.url })
  }
})

since you're using json.url above, it expects you to return the url in the json response so in your controller, add the following to the respond_to block

format.json { render json: { url: edit_document_path(@document) } }

Post a Comment for "Jquery File Upload Rails Redirect"