我正在尝试做的是通过 ajax 登录,但如果身份验证失败则返回表单(有错误)。默认情况下,devise
将返回一般错误。这是我的控制器:
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
然而这会返回一个错误:
"You need to sign in or sign up before continuing."
我的js:
$('#login-form').bind 'ajax:success', (xhr, data, status) ->
alert data.content
有人可以帮助正确地构建这个吗?