How can I elegantly handle devise's 401 status in AJAX?

后端 未结 7 1965
庸人自扰
庸人自扰 2020-12-23 18:53

With devise one uses before_filter :authenticate_user! to restrict access to authenticated users only.

When an unauthenticated

7条回答
  •  温柔的废话
    2020-12-23 19:46

    You can use the .live on the "ajax:error" event binding if you are doing ":remote => true".

    $('#member_invite, #new_user')
            .live("ajax:success", function(evt, data, status, xhr){
              $.colorbox.close();
            })
            .live("ajax:error", function(evt, data, status, xhr){
              alert("got an error");
            });
    

    where the "#new_user" would be the form ID value.

    Note that a more elegant way if you already have an overlay or dialog is simply to insert a message, so instead of alert():

    $('.messages').html('Invalid email or password');
    

    and in your signin form you just do a

    Or you could even just replace the title of the form, what ever your needs are.

提交回复
热议问题