使用rails 3和remote =>真正 [英] Devise with rails 3 and remote => true

查看:88
本文介绍了使用rails 3和remote =>真正的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用设计与AJAX登录的问题。我正在使用:remote =>真正的选项和jQuery版本的javascript助手(https://github.com/rails/jquery-ujs)。
Ehen用户输入正确的信息,会话视图中的create.js.erb被渲染。这是确定的,因为我可以使用JS在这个文件中重定向我的用户。但是当发生错误时,例如用户输入错误信息,只有Flash消息在响应中,错误代码为 401 - 未经授权。所以,没有view或者create.js.erb或者sth else被渲染。
但是我想处理这个消息,通过显示在一边,以便用户获得一些反馈,有什么问题。

I have a problem using devise with an AJAX login. I'm using the :remote => true option and the jQuery version of the javascript helpers (https://github.com/rails/jquery-ujs). Ehen the user enters the correct informations, my create.js.erb inside the sessions view is rendered. This is ok, 'cause i can redirect my user using JS in this file. But when an error occurs, e.g. the user enters false information, only the flash messages are in the response with an error code 401 - Unauthorized. So, no view or create.js.erb or sth else is rendered. But I want to handle this message, by displaying it on the side, so that the users gets some feedback, whats wrong.

我也试图实现我自己的自定义控制器,具有 respond_to:js ,并使用守护程序认证的:Recall 方法。但是它不起作用。

I also tried to implement my own custom controller with respond_to :js, and playing with the :recall method of the warden authentication. But it doesn't work.

有没有人知道一些最佳做法来解决这个问题?或者我应该避免这种帮助方法,只是使用纯jQuery进行ajax调用?

Does anybody know some best practices to solve this problem? Or should I avoid this helper methods and just use pure jQuery for the ajax calls?

thx,
tux

thx, tux

推荐答案

您可以在rails 3中使用jQuery事件ajax:error。只需将事件绑定到您的表单。

You could use the jQuery event "ajax:error" in rails 3. Just bind the event to your form.

jQuery(function($) {
  $("#new_model").bind("ajax:error", function(event, data, status, xhr) {
    alert("something went wrong");
  });
});

看看这个博客文章了解更多有关哪些事件可用的详细信息。

Take a look at this blog post for more details on what events are available.

这篇关于使用rails 3和remote =>真正的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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