我想要做的是通过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
并按照此处的建议进行自定义: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
有人可以帮助正确构建这个吗?
答案 0 :(得分:1)
Trick是改变设计配置并注册:
config.http_authenticatable_on_xhr = false
config.navigational_formats = ["*/*", :html, :json]