1

I am using Turbolinks in my rails project . When I clicked upload button(ajax call),it fails and the error section is showing Uncaught TypeError: Cannot read property 'length' of undefined. The same problem is showing with regular link(no ajax) which is caused by turbolinks I think because turbolinks uses ajax in the background.

my upload page's javascript:

<html>
..... html code ...

<%= javascript_include_tag 'js/vendor/jquery.ui.widget.js' %>
<%= javascript_include_tag "js/jquery.iframe-transport" %>
<%= javascript_include_tag "js/jquery.fileupload.js" %>

<script>
$(function () {
    'use strict';
    var filestoupload = 0;
    $('#fileupload').fileupload({
        dataType: 'json',
         add: function(e, data) {
                if(data.files[0]['size'] > 20000000) {
                   $('#errors_append').html('maximum size for file allowed is 20 mb');
                } else {
                    data.submit();
                }
        },
        done: function (e, data) {
            $.each(data.result.files, function (index, file) {
                filestoupload++;
                $('').text(file.name + ' ' + 'uploaded successfully').appendTo('#files_append');

                if (filestoupload > 2) {
                  $('#file_button').attr("disabled","disabled");
                }
            });
            $("#btn_text").html('Add more files');
        },
        start: function (e, data) {
            $('#progress .progress-bar').css(
                'width',
                0 + '%'
                );   
        },
        progressall: function (e, data) {
            var progress = parseInt(data.loaded / data.total * 100, 10);
            $('#progress .progress-bar').css(
                'width',
                progress + '%'
                );
        },
        success: function(xhr){
          $('#progress .progress-bar').attr('class', 'progress-bar progress-bar-success');
          $('#errors_append').empty();      
      },
      error: function(xhr){
          var errors = $.parseJSON(xhr.responseText).error
          $('#errors_append').html(errors);
          $('#progress .progress-bar').attr('class', 'progress-bar progress-bar-danger');        
      }
  }).prop('disabled', !$.support.fileInput)
.parent().addClass($.support.fileInput ? undefined : 'disabled');
});
</script>
7
  • where is your js/jquery.fileupload.js file in your directory? and why are you using js in your view? Commented Jul 5, 2014 at 18:09
  • in /app/assets/javascripts/js/jquery.fileupload.js Commented Jul 5, 2014 at 18:12
  • can you post exact error that you are getting in your developer console Commented Jul 5, 2014 at 18:21
  • I'have added screenshots but i dont have enough reputaion point :( , Commented Jul 5, 2014 at 18:40
  • 1
    No , but when i remove turbolinks, it works fine, or when i'have included the js in application.js(require) it works also fine even with turbolinks. But currently i dont know exactly why. Commented Jul 6, 2014 at 1:43

1 Answer 1

2

We got jquery-file-upload working with ajax here (sign up for account then try updating your profile pic). Model is Veronica Crespo:

enter image description here

--

Turbolinks

The main issue with Turbolinks is that it will prevent your on-page JS from loading the DOM elements it requires.

Because Turbolinks just refreshes the <body> element of your page (not the <head>), JS still thinks that it's in the "old" state, preventing it from picking up any of the newly-loaded elements.

Its fixed by either delegating your JS from a constant element (typically document), or by using the Turbolinks event hooks to fix it:

var load = function() {
   ...
};

$(document).on("page:load ready", load);

--

Fix

In regards to working with Turbolinks, here's the code we used for the Avatar functionality in one of our demo apps:

#app/assets/javascripts/application.js
$('#avatar').fileupload({

    url: '/profile/' + $(this).attr('data_id'),
    dataType: 'json',
    type: 'post',

    add: function (e, data) {
        //$(".items .avatar .avatar").prepend('<div class="loading" id="avatar_loading"><img src="<%= asset_path("profile/avatar_loading.gif") %>"></div>');
        //$('#avatar_loading').fadeIn('100');
        $(this).avatar_loading('avatar_loading');
        data.submit();
    },
    success: function (data, status) {;
        $("#avatar_img").fadeOut('fast', function() {
            $(this).attr("src", data.avatar_url).fadeIn('fast', function(){
                $(this).avatar_loading('avatar_loading');
            });
        });
    }

});

This is in line with the jquery-fileupload gem:

#Gemfile
gem "jquery-fileupload-rails", "~> 0.4.1"

#app/assets/javascripts/application.js
//= require jquery-fileupload/basic
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.