2

我正在尝试做的是通过 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

有人可以帮助正确地构建这个吗?

4

1 回答 1

1

诀窍是更改设计配置并注册:

config.http_authenticatable_on_xhr = false
config.navigational_formats = ["*/*", :html, :json]
于 2013-02-07T10:46:07.870 回答