Jquery bind on 'ajax:success', 'ajax:error' or 'ajax:complete' not executed even though form is submitted
ajax, devise, jquery, ruby-on-rails, ruby-on-rails-3
Solution
In Jquery, Ajax Event has two types. One is local event, another is global event.
Local Event, It will be defined with ajax request:
$.ajax('YOUR PATH', {success: function(){ alert(!) }});
Global Event, It should be binded with document element:
$(document).bind('ajaxSuccess', function() {
alert('!');
});
see fiddle and jquery api
Problem
In my Jquery bind method , `'ajax:success', 'ajax:error' or 'ajax:complete'` callbacks are not being excuted. The form is being submitted through Ajax and the data is inserted to the database successfully but those callbacks function are not being invoked at all (its simply supposed to show alert pop up inside those callbacks.) I am using Jquery 1.6,Rails 3.1 Here is my code.Thank you in advance. Form ``` <%= form_for(resource,:as=>resource_name,:url => registration_path(resource_name), :html => {:id => "sign_up_user"},:remote => true,:format => :json) do |f| %> <%= devise_error_messages! %> <%= f.label :Username %> <%= f.text_field :username %></p> <%= f.label :email %> <%= f.text_field :email %> <%= f.label :password %> <%= f.password_field :password %> <%= f.label :password_confirmation %> <%= f.password_field :password_confirmation %> <%= f.submit "Sign up" ,:id=>"sign-up" %> <% end %> ``` Ajax bind ``` $('#sign_up_user').bind('ajax:success', function( data, status, xhr) { alert("success"); }); ``` Controller ``` class RegistrationsController < Devise::RegistrationsController def create build_resource if resource.save if resource.active_for_authentication? set_flash_message :notice, :signed_up if is_navigational_format? sign_up(resource_name, resource) return render :json => {:success => true} else set_flash_message :notice, :"signed_up_but_#{resource.inactive_message}" if is_navigational_format? expire_session_data_after_sign_in! return render :json => {:success => true} end else clean_up_passwords resource return render :json => {:success => false} end end # Signs in a user on sign up. You can overwrite this method in your own RegistrationsController def sign_up(resource_name, resource) sign_in(resource_name, resource) end end ``` Log ``` Started POST "/users" for 127.0.0.1 at 2013-08-13 22:12:51 -0500 Processing by Devise::RegistrationsController#create as JS Parameters: {"utf8"=>"✓", "authenticity_token"=>"mrcZMq5WT1QPNpGDGsVFWIPx+WqfI0PZmHfGqs7jnrM=", "user"=> {"username"=>"ksks", "email"=>"jsjs@t.comq", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "address"=>"", "business"=>"0"}, "commit"=>"Sign up"} (0.2ms) SELECT 1 FROM "users" WHERE LOWER("users"."email") = LOWER('jsjs@t.comq') LIMIT 1 SQL (2.6ms) INSERT INTO "users" ("address", "business", "business_name", "business_type", "city", "created_at", "current_sign_in_at", "current_sign_in_ip", "email", "encrypted_password", "last_sign_in_at", "last_sign_in_ip", "phone", "remember_created_at", "remember_token", "reset_password_token", "sign_in_count", "state", "updated_at", "username", "zipcode") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) [["address", nil], ["business", false], ["business_name", nil], ["business_type", nil], ["city", nil], ["created_at", Wed, 14 Aug 2013 03:12:52 UTC +00:00], ["current_sign_in_at", nil], ["current_sign_in_ip", nil], ["email", "jsjs@t.comq"], ["encrypted_password", "$2a$10$uynKQ8s3sjBe5W3LfqFxSO9Z2jg9FB.m8LbVOiiBNqDtT9qF1j7Sy"], ["last_sign_in_at", nil], ["last_sign_in_ip", nil], ["phone", nil], ["remember_created_at", nil], ["remember_token", nil], ["reset_password_token", nil], ["sign_in_count", 0], ["state", nil], ["updated_at", Wed, 14 Aug 2013 03:12:52 UTC +00:00], ["username", "ksks"], ["zipcode", nil]] (0.4ms) UPDATE "users" SET "last_sign_in_at" = '2013-08-14 03:12:52.570849', "current_sign_in_at" = '2013-08-14 03:12:52.570849', "last_sign_in_ip" = '127.0.0.1', "current_sign_in_ip" = '127.0.0.1', "sign_in_count" = 1, "updated_at" = '2013-08-14 03:12:52.572092' WHERE "users"."id" = 35 ```