如何处理devise的authenticate_user!用ajax调用? [英] how to handle devise's authenticate_user! with ajax call?
问题描述
我有一个表单:remote => true表示将通过ajax提交。
I have a form with :remote => true which means it will be submitted through ajax.
在控制器中,我有这样的代码:
In the controller, I have such code:
before_filter: authenticate_user!, :only => [:create]
因为我只允许确认的用户创建资源。
For I only allow confirmed user to create resource.
但是,当身份验证失败时,设计将会提高
However, when the authentication fails, devise will raise a
Completed 401 Unauthorized
并且不会显示不引人注目的JavaScript。
and no unobtrusive javascript will be rendered.
但是希望如此:
设计在flash中设置一些消息,并渲染我的.js.erb,然后我向用户显示闪光。
devise set some messages in flash, and render my .js.erb, then I show the flash to users.
如何实现?
推荐答案
这是非常有趣但是在devise.rb中更改http_authenticatable_on_xhr到false
It is quite funny but change in devise.rb http_authenticatable_on_xhr to false
config.http_authenticatable_on_xhr = false
为我做的窍门...没有覆盖authenticate_user!方法
did the trick for me... without overriding authenticate_user! method
注意:我还添加了:js到导航格式
note: I also added :js to navigational formats
config.navigational_formats = [:"*/*", "*/*", :html, :js]
这篇关于如何处理devise的authenticate_user!用ajax调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!