这是我的会话控制器:
class SessionsController < Devise::SessionsController
respond_to :json
def create
resource = warden.authenticate!(scope: resource_name, recall: "#{controller_path}#failure")
sign_in(resource_name, resource)
return render json: { success: true, path: root_path }
end
def failure
return render json: { success: false, errors: ['Login information is incorrect, please try again'] }
end
end
用户#active_for_authentication
def active_for_authentication?
super && active_flag == 1 && has_active_subscription?
end
服务器响应:
Started POST "/users/sign_in" for 127.0.0.1 at 2013-07-29 15:11:39 -0500
Processing by SessionsController#create as JSON
Parameters: {"utf8"=>"✓", "authenticity_token"=>"ozQ00tw9rRWExCmlIyIZR07ovnTLab5w0W44cLAKwA4=", "user"=>{"email"=>"dennis+dlksjfdkfjsd@recruittalk.com", "password"=>"[FILTERED]"}, "commit"=>"Sign In"}
User Load (1.0ms) SELECT "users".* FROM "users" WHERE "users"."type" IN ('User', 'Athlete', 'Coach') AND "users"."email" = 'dennis+dlksjfdkfjsd@recruittalk.com' LIMIT 1
(0.6ms) BEGIN
AccountType Load (0.5ms) SELECT "account_types".* FROM "account_types" WHERE "account_types"."id" = $1 LIMIT 1 [["id", 3]]
AccountType Load (0.7ms) SELECT "account_types".* FROM "account_types" WHERE "account_types"."id" = 3 LIMIT 1
(0.5ms) COMMIT
Completed 401 Unauthorized in 338ms
Started GET "/users/sign_in.json" for 127.0.0.1 at 2013-07-29 15:11:40 -0500
Processing by SessionsController#new as JSON
Completed 200 OK in 13ms (Views: 1.6ms | ActiveRecord: 0.0ms)
由于某种原因,浏览器中的请求包含一个带有电子邮件和密码作为属性的用户对象。无论出于何种原因,失败的 json 都不会呈现。不知道我做错了什么
编辑
#Devise
config.http_authenticatable_on_xhr = false
咖啡脚本:
$(document).on 'ajax:success', '.user_modal_form', (e, data) ->
context = $(this)
if data.success
$('input[type="submit"]', context).hide()
$('.spinner', context).show()
if data.path?
window.location.href = data.path
else
location.reload()
else
e.preventDefault()
if data.errors?
$('.error-messages', context).html(data.errors).parent().show()
else
$('button', context).show()
$('.spinner', context).hide()
$('.alert', context).show()
false