Skip to content

Instantly share code, notes, and snippets.

@Anuragjain89
Last active August 29, 2015 14:23
Show Gist options
  • Select an option

  • Save Anuragjain89/d48aa393b49eec474c84 to your computer and use it in GitHub Desktop.

Select an option

Save Anuragjain89/d48aa393b49eec474c84 to your computer and use it in GitHub Desktop.
Ajax file upload with remote:true

Update on file upload using ajax.

While uploading the file with remote true,.. the system would require us to specify authenticity token. Otherwise, you will get error stating invalid authenticity token.

<%= nested_form_for [@project, @test_run, @test_runs_test_case], remote:true, authenticity_token: true do |f| %>

Internally the request is sent as HTML instead of xhr when the params contains any new attachment.

      if (remote) {
        nonBlankFileInputs = rails.nonBlankInputs(form, rails.fileInputSelector);
        if (nonBlankFileInputs) {
          // slight timeout so that the submit button gets properly serialized
          // (make it easy for event handler to serialize form without disabled values)
          setTimeout(function(){ rails.disableFormElements(form); }, 13);
          var aborted = rails.fire(form, 'ajax:aborted:file', [nonBlankFileInputs]);

          // re-enable form elements if event bindings return false (canceling normal form submission)
          if (!aborted) { setTimeout(function(){ rails.enableFormElements(form); }, 13); }

          return aborted;
        }

        rails.handleRemote(form);
        return false;

      } else {
        // slight timeout so that the submit button gets properly serialized
        setTimeout(function(){ rails.disableFormElements(form); }, 13);
      }
    });

#####PLAIN TEXT WITHOUT ANY ADDITION / DELETION OF ATTACHMENTS

Started PATCH "/projects/1/test_runs/4/test_runs_test_cases/7" for 127.0.0.1 at 2015-06-20 17:03:53 +0530-- ActiveRecord::SchemaMigration Load (0.3ms) SELECT "schema_migrations".* FROM "schema_migrations"-- Processing by TestRunsTestCasesController#update as JS--

Parameters: {"utf8"=>"✓", "authenticity_token"=>"IisbUst7jdpQryeCbWlpZYPzsq3NlMPUhkwB7C3r8lpKzNlB0ZHSmToLEV1eOGAgbagD4PnL/j7/KY5xGsKYyw==", "test_runs_test_case"=>{"comment"=>"ssss"}, "admin_request"=>"", "multiple_test_case"=>"true", "next_id"=>"8", "commit"=>"Pass", "project_id"=>"1", "test_run_id"=>"4", "id"=>"7"}--

(byebug) request.xhr? 0

#####ADDING NEW ATTACHMENTS

Started PATCH "/projects/1/test_runs/4/test_runs_test_cases/7" for 127.0.0.1 at 2015-06-20 17:06:23 +0530-- Processing by TestRunsTestCasesController#update as HTML--

Parameters: {"utf8"=>"✓", "authenticity_token"=>"bLaYLvHJpBHKsZnflC2GZ4rH0T2qDJwVXbkAdatZj1oEUVo96yP7UqAVrwCnfI8iZJxgcJ5Tof8k3I/onHDlyw==", "test_runs_test_case"=>{"comment"=>"", "test_runs_test_case_attachments_attributes"=>{"1434800174215"=>{"attachment"=>#<ActionDispatch::Http::UploadedFile:0x007fb6fc19b9e0 @tempfile=#Tempfile:/tmp/RackMultipart20150620-13062-q9ne2g.png, @original_filename="Slack.png", @content_type="image/png", @headers="Content-Disposition: form-data; name="test_runs_test_case[test_runs_test_case_attachments_attributes][1434800174215][attachment]"; filename="Slack.png"\r\nContent-Type: image/png\r\n">, "_destroy"=>"false"}}}, "commit"=>"Pass", "admin_request"=>"", "multiple_test_case"=>"true", "next_id"=>"8", "project_id"=>"1", "test_run_id"=>"4", "id"=>"7"}--

(byebug) request.xhr? nil

#####DELETING EXISTING ATTACHMENTS

Started PATCH "/projects/1/test_runs/4/test_runs_test_cases/7" for 127.0.0.1 at 2015-06-20 17:08:35 +0530-- Processing by TestRunsTestCasesController#update as JS--

Parameters: {"utf8"=>"✓", "authenticity_token"=>"yCNcw+UWWInIV/k18w6OR+8iL73GqVCF7LjD8Hw0wtSgxJ7Q//wHyqLzz+rAX4cCAXme8PL2bW+V3UxtSx2oRQ==", "test_runs_test_case"=>{"comment"=>"", "test_runs_test_case_attachments_attributes"=>{"0"=>{"_destroy"=>"1", "id"=>"3"}}}, "admin_request"=>"false", "multiple_test_case"=>"false", "next_id"=>"", "commit"=>"Fail", "project_id"=>"1", "test_run_id"=>"4", "id"=>"7"}--

(byebug) request.xhr? 0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment