如何处理设备的authenticate_user!用ajax调用? [英] how to handle devise's authenticate_user! with ajax call?

查看:16
本文介绍了如何处理设备的authenticate_user!用ajax调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有 :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.

但是,当认证失败时,devise 会引发一个

However, when the authentication fails, devise will raise a

Completed 401 Unauthorized

并且不会呈现不显眼的 javascript.

and no unobtrusive javascript will be rendered.

但我希望事情是这样的:

But I hope things goes like this:

设计在flash中设置一些消息,并渲染我的.js.erb,然后我向用户展示flash.

devise set some messages in flash, and render my .js.erb, then I show the flash to users.

如何实现?

推荐答案

很有趣但是在 devise.rb http_authenticable_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]

这篇关于如何处理设备的authenticate_user!用ajax调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆