2012-03-09 18 views
5

sto sotto:
Rails 3.0.9
Devise 1.4.7
jquery-UJSRails Messaggio 3 Devise Ajax login Сatching errore

Che cosa ho fatto ...
1) ho? imposta il modulo di accesso con data-remote = "true".
2) Ho installato il segno-in jQuery: gestori

('form#user-reg-form') 
    .bind("ajax:beforeSend", function(evt, xhr, settings){ 
    }) 
    .bind("ajax:success", function(evt, data, status, xhr){ 
     $('#comments').append(xhr.responseText); 

    }) 
    .bind('ajax:complete', function(evt, xhr, status){ 
    }) 
    .bind("ajax:error", function(evt, xhr, status, error){ 
     try { 
     errors = $.parseJSON(xhr.responseText); 
     } catch(err) { 
     errors = {message: "Please reload the page and try again"}; 
     } 
     errorText = "There were errors with the submission: \n<ul>"; 

     for (error in errors) { 
     errorText += "<li>" + error + ': ' + errors[error] + "</li> "; 
     } 

     errorText += "</ul>"; 

     // Insert error list into form 
     $form.find('div.validation-error').html(errorText); 
    }); 

3) ho ridefinito il controller di sessione in questo modo:

def create 
    resource = warden.authenticate!(:scope => resource_name, :recall => "#{controller_path}#failure") 
    sign_in(resource_name, resource) 
    return render :json => { :success => true, :content => current_user.email } 
    end 

    def failure 
    return render:json => {:success => false, :errors => ["Login failed."]} 
    end 

4) ho impostato il mio l'route.rb File:

devise_for :users, :controllers => {:sessions => "sessions"} 

Tutto funziona ... ma quando passo e-mail o password errata devo messaggio non rilevata dal modello utente (forse da Devise) "e-mail o la password sono errati". Ma voglio prendere questo messaggio e metterlo in json object qualcosa di simile al rendering di ritorno : json => {: success => falso,: errors => //// email o password errata ////}

Il problema è: : il richiamo non funziona. Sembra che Warden.authenticate! non vede il mio metodo di errore

Grazie in anticipo.

risposta

4

Le soluzioni era molto semplice)
devo impostare in config/inizializzatori/devise.rb:

config.http_authenticatable_on_xhr = false 
config.navigational_formats = [:html, :json] 
Problemi correlati