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
new
和create
動作從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
有人可以幫忙正確地構造這個?
使用Backbone.js的與服務器進行通信時,這也適用。感謝你! – jwilcox09