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 に答える