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 the file:
return data.submit();
After the file has finished uploading, how can I redirect to the edit_document_path, i have tried the following code:
$('#fileupload')
.bind('fileuploadstop', function (e, data) {
window.location.href = "#{edit_document_path(@document)}";
})
Is there a way I can store or return the ID (or even better the @document itself) of the uploaded document so I can redirect through js?
EDIT
I have my js directly in the html.haml file
%script(type="text/javascript")
$('#new_document').fileupload({
dataType: 'json',
add: function (e, data) {
jqXHR = data.submit()
},
done: function (e, data) {
window.location = json.url
},
progress: function (e, data) {
var progress = parseInt(data.loaded / data.total * 100, 10);
$('.bar').css('width', progress + '%');
}
});
and here is my controller:
def upload
@document = Document.new(params[:document])
respond_to do |format|
format.json { render json: { url: edit_document_path(@document) } }
end
end
However, the JSON response does not seem to be working as the page does not redirect, what am I doing wrong?
Thankyou
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.
you also need to use
<%= %>
if you are usingerb
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 bejson
.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 therespond_to
block