Devise with rails 3 and remote => true
I have a problem using devise with an AJ开发者_StackOverflowAX 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.
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.
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
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.
Here's my version
<script>
$(document).ajaxError(function(e, XHR, options){
if (XHR.status == 401){
window.location.replace("<%= new_user_session_path %>");
}
});
</script>
It automatically redirects the user to the login page and works for all elements on the page.
精彩评论