2013-02-06 74 views
2

我想要做的是通過ajax登錄,但如果身份驗證失敗,則返回表單(帶有錯誤)。默認情況下,devise將返回一個通用錯誤。這裏是我的控制器:無法設計Ajax工作

class SessionsController < Devise::SessionsController 
    respond_to :js 

    def new 
    self.resource = build_resource(nil, :unsafe => true) 
    clean_up_passwords(resource) 
    render_user_form(resource, false) 
    end 

    def create 
    self.resource = warden.authenticate!(scope: resource_name, recall: "#{controller_path}#new") 
    sign_in(resource_name, resource) 
    render_user_form(resource, true) 
    end 

    def render_user_form(resource, success) 
    render json: { 
     success: success, 
     content: render_to_string(partial: 'users/login_form', locals: { user: resource }) 
    } 
    end 

end 

newcreate動作從Devise::SessionsController採取和定製,作爲建議在這裏:custom sign in for devise

然而,這會返回一個錯誤:

"You need to sign in or sign up before continuing." 

我的JS:

$('#login-form').bind 'ajax:success', (xhr, data, status) -> 
    alert data.content 

有人可以幫忙正確地構造這個?

回答

1

把戲是改變色器件配置和註冊:

config.http_authenticatable_on_xhr = false 
config.navigational_formats = ["*/*", :html, :json] 
+0

使用Backbone.js的與服務器進行通信時,這也適用。感謝你! – jwilcox09