AJAX error handling

This commit is contained in:
Erik Michaels-Ober 2011-03-06 18:40:29 -08:00
parent b61f81a9d0
commit 6027e5c6f9
6 changed files with 100 additions and 18 deletions

View File

@ -0,0 +1,32 @@
class PasswordsController < Devise::PasswordsController
respond_to :json, :only => [:create, :update]
# POST /resource/password
def create
self.resource = resource_class.send_reset_password_instructions(params[resource_name])
if resource.errors.empty?
render(:json => {"success" => true})
else
render(:json => {"errors" => resource.errors})
end
end
# GET /resource/password/edit?reset_password_token=abcdef
def edit
self.resource = resource_class.new
resource.reset_password_token = params[:reset_password_token]
render_with_scope :edit
end
# PUT /resource/password
def update
self.resource = resource_class.reset_password_by_token(params[resource_name])
if resource.errors.empty?
render(:json => {"success" => true})
else
render(:json => {"errors" => resource.errors})
end
end
end

View File

@ -3,13 +3,17 @@ class SessionsController < Devise::SessionsController
def create
resource = warden.authenticate(:scope => resource_name)
sign_in(resource_name, resource)
respond_with resource
if resource
sign_in(resource_name, resource)
respond_with resource
else
render(:json => {"errors" => {:password => ["You need to sign in or sign up before continuing."]}})
end
end
def destroy
signed_in = signed_in?(resource_name)
sign_out(resource_name) if signed_in
respond_with signed_in
render(:json => {"success" => signed_in})
end
end

View File

@ -3,16 +3,16 @@ class UsersController < Devise::RegistrationsController
def create
build_resource
if resource.save
if resource.active?
sign_in(resource_name, resource)
else
expire_session_data_after_sign_in!
end
respond_with resource
else
clean_up_passwords(resource)
render(:json => {"errors" => resource.errors})
end
respond_with resource
end
end

View File

@ -58,8 +58,8 @@
$('#user_email_label').addClass('error', 500);
$('#user_email').addClass('error', 500);
} else {
$('#user_email_label').removeClass('error', 500);
$('#user_email').removeClass('error', 500);
$('#user_email_label').removeClass('error');
$('#user_email').removeClass('error');
}
if($(this).data('state') === 'user_new') {
if($('#user_name').val() === '') {
@ -67,16 +67,16 @@
$('#user_name_label').addClass('error', 500);
$('#user_name').addClass('error', 500);
} else {
$('#user_name_label').removeClass('error', 500);
$('#user_name').removeClass('error', 500);
$('#user_name_label').removeClass('error');
$('#user_name').removeClass('error');
}
if($('#user_password_confirmation').val().length < 6 || $('#user_password_confirmation').val().length > 20) {
errors.push($('#user_password_confirmation'));
$('#user_password_confirmation_label').addClass('error', 500);
$('#user_password_confirmation').addClass('error', 500);
} else {
$('#user_password_confirmation_label').removeClass('error', 500);
$('#user_password_confirmation').removeClass('error', 500);
$('#user_password_confirmation_label').removeClass('error');
$('#user_password_confirmation').removeClass('error');
}
if(errors.length > 0) {
errors[0].focus();
@ -95,16 +95,50 @@
'password_confirmation' : $('#user_password_confirmation').val()
}
}, function(data) {
if(data.errors) {
if(data.errors.email) {
errors.push($('#user_email'));
$('#user_email_label').addClass('error', 500);
$('#user_email').addClass('error', 500);
}
if(data.errors.name) {
errors.push($('#user_name'));
$('#user_name_label').addClass('error', 500);
$('#user_name').addClass('error', 500);
}
if(data.errors.organization) {
errors.push($('#user_organization'));
$('#user_organization_label').addClass('error', 500);
$('#user_organization').addClass('error', 500);
}
if(data.errors.voice_number) {
errors.push($('#user_voice_number'));
$('#user_voice_number_label').addClass('error', 500);
$('#user_voice_number').addClass('error', 500);
}
if(data.errors.sms_number) {
errors.push($('#user_sms_number'));
$('#user_sms_number_label').addClass('error', 500);
$('#user_sms_number').addClass('error', 500);
}
if(data.errors.password) {
errors.push($('#user_password_confirmation'));
$('#user_password_confirmation_label').addClass('error', 500);
$('#user_password_confirmation').addClass('error', 500);
}
errors[0].focus();
} else {
}
});
}
} else if($(this).data('state') === 'user_existing') {
if($('#user_password').val() === '') {
if($('#user_password').val().length < 6 || $('#user_password').val().length > 20) {
errors.push($('#user_password'));
$('#user_password_label').addClass('error', 500);
$('#user_password').addClass('error', 500);
} else {
$('#user_password_label').removeClass('error', 500);
$('#user_password').removeClass('error', 500);
$('#user_password_label').removeClass('error');
$('#user_password').removeClass('error');
}
if(errors.length > 0) {
errors[0].focus();
@ -118,6 +152,12 @@
'password' : $('#user_password').val()
}
}, function(data) {
if(data.errors) {
$('#user_password').focus();
$('#user_password_label').addClass('error', 500);
$('#user_password').addClass('error', 500);
} else {
}
});
}
} else if($(this).data('state') === 'user_forgot_password') {
@ -132,6 +172,12 @@
'email' : $('#user_email').val()
}
}, function(data) {
if(data.errors) {
$('#user_email').focus();
$('#user_email_label').addClass('error', 500);
$('#user_email').addClass('error', 500);
} else {
}
});
}
}

View File

@ -5,8 +5,8 @@ class DeviseCreateUsers < ActiveRecord::Migration
t.string :name, :null => false
t.string :organization
t.string :email, :null => false
t.integer :voice_number
t.integer :sms_number
t.string :voice_number
t.string :sms_number
t.database_authenticatable :null => false
t.recoverable
t.rememberable

View File

@ -30,8 +30,8 @@ ActiveRecord::Schema.define(:version => 20110223180521) do
t.string "name", :null => false
t.string "organization"
t.string "email", :default => "", :null => false
t.integer "voice_number"
t.integer "sms_number"
t.string "voice_number"
t.string "sms_number"
t.string "encrypted_password", :limit => 128, :default => "", :null => false
t.string "password_salt", :default => "", :null => false
t.string "reset_password_token"