【发布时间】:2013-12-21 20:09:15
【问题描述】:
我正在尝试使用 devise 进行 ajax 登录,但我似乎无法正常工作。问题是我在设计上启用了可确认的策略,warden.authenticate!应该在非活动用户上失败并呈现failure 操作,而不是呈现默认操作。我注意到许多教程都有相同的设置。我正在使用 rails 4.0.0 并设计 3.2.2。
class SessionsController < Devise::SessionsController
def create
#problem: failure not getting called
resource = warden.authenticate!(scope: resource_name, recall: "#{controller_path}#failure")
sign_in(resource_name, resource)
render json: {success: true}
end
def failure
render json: {success: false, errors: ["Login failed!"]}
end
end
devise.rb 设置如下,
config.http_authenticatable_on_xhr = false
config.navigational_formats = ['*/*', :'*/*', :html, :json]
routes.rb 有以下内容,
devise_for :users, controllers: {registrations: "registrations", sessions: "sessions"}
登录表单,
<%= form_for(resource, as: resource_name,
url: session_path(resource_name),
format: :json,
html:{id:"signin_form"},
remote: true) do |f| %>
<%= f.label(:email, "Email") %>
<%= f.text_field(:email, class: "field text") %>
<%= f.label(:password, "Password") %>
<%= f.text_field(:password, class: "field text", id: "user_signin_password") %>
<%= f.submit "Sign in", class: "theme_color btn key" %>
<% end %>
由于failure 动作没有被调用,默认的new 动作呈现如下响应头,
Content-Type text/html; charset=utf-8
Location http://localhost:3000/users/sign_in.js
我也很好奇为什么它呈现.js而不是.json。
谁能指出我错过了什么?谢谢
【问题讨论】:
标签: ruby-on-rails ruby json devise ruby-on-rails-4