1

I googled a lot and was unable to find my mistake. As I just started with JS this might be an easy one. Using: Rails 4

I have a modal with a form that I would like to do stuff after successfully (or not) doing the ajax call. For now just closing the modal.

modal (this works actually works, so I can edit stuff):

.modal.fade{ id: attribute.id, role: 'dialog', tabindex:'-1' }
  .modal-dialog{ role: 'document'}
    .modal-content
      .modal-header
        %button{ type: 'button', class: 'close', 'data-dismiss' => "modal" }
          %span ×
        %h4.modal-title= attribute.name

  = form_for attribute, remote: true, html: { class: 'form-inline', id: 'edit_form' } do |f|
    .modal-body
      .form-group
        = f.label :name, 'Name', class: 'form-label', style: 'width: 150px'
        = f.text_field :name, class: 'form-control', style: 'width:200px;margin-right: 35px'
    .modal-footer
      %button{ type: "button", class: "btn btn-default pull-left", 'data-dismiss' => "modal" }
        Close
      = f.submit 'Create', class: 'btn btn-primary pull-right'

my JS file (dont laugh):

$(document).ready( function($) {
        console.log('ready'); # this I *can* see in the console

    $('#edit_form').bind("ajax:success", function() {
      console.log('great success') # this I *can't* see in the console.
    });
});

controller (additional fun fact: The modal is triggered in a view for a different controller, not the one of the modal that I am actually trying to change):

respond_to do |format|
  format.js {}
end

So in the end I would like to catch the "error" and "success" callbacks and do sth with it (this I am trying to figure out myself first). What am I doing wrong here?

Thanks a lot for your help - it is really appreciated!

tomr
  • 1,134
  • 2
  • 10
  • 25

1 Answers1

1

According to the answer of a similar question you might want to try:

$('#edit_form').on("ajax:success", ...
//             ^-- instead of .bind

I assume you also have a recent jQuery version, so give this change a shot.

Community
  • 1
  • 1
asaaki
  • 1,970
  • 18
  • 22