简体   繁体   English

使用Ajax提交我的Devise登录表单后,如何使jQuery .on()工作?

[英]How can I get jQuery .on() to work after submitting my Devise sign in form using Ajax?

There are a number of questions about this on SO but none of them has provided a surefire answer for me. 关于SO的问题很多,但没有一个为我提供肯定的答案。 In my Rails 3.2 app the user has the option to create a new User or sign in as an existing User at the end of a certain form. 在我的Rails 3.2应用程序中,用户可以选择创建新用户,也可以在某种形式的末尾以现有用户身份登录。 To allow existing users to sign in, I have implemented a bootstrap modal with a Devise sign in form that submits via Ajax to my custom SessionsController. 为了允许现有用户登录,我实现了具有Devise登录表单的引导程序模式,该表单通过Ajax提交到我的自定义SessionsController。 The Ajax request is working correctly and signing in a user without leaving the page, but I am having trouble getting my jQuery .on() handler to work so that some things on the page change after the successful request takes place. Ajax请求可以正常工作,并且无需离开页面即可登录用户,但是我无法使jQuery .on()处理程序正常工作,因此成功请求发生后页面上的某些内容会发生变化。 Below I have posted my code for the form, the RegistrationsController, and the jQuery snippet in a form that is just supposed to give an alert message when the sign in has an error (haven't gotten even that much to work). 在下面,我以表单的形式发布了表单,RegistrationsController和jQuery代码段的代码,该表单应该在登录出错(甚至无法正常工作)时发出alert消息。

I have tried adding something like render :json => {:foo => bar} to the controller because on some of the other questions people were saying that something has to be rendered for the request to be considered successful but that didn't fix my problem. 我曾尝试向控制器添加诸如render :json => {:foo => bar} ,因为在其他一些问题上,人们说必须渲染某些东西才能使请求被认为是成功的,但是并没有解决我的问题。

_new_signin.html.haml
...
      .modal-body
        = form_for(resource, :as => resource_name,
                     :url => session_path(resource_name) ,
                     :html => {:id => "sign_in_user"},
                     :format => :json,
                     :remote => true ) do |signin_form|
          .form-group 
            = signin_form.label :email
            = signin_form.email_field :email, class: 'form-control'
          .form-group
            = signin_form.label :password
            = signin_form.password_field :password, class: 'form-control'
          .form-group
            = signin_form.label :remember_me
            = signin_form.check_box :remember_me
          .form-group
            = link_to "Forgot your password?", new_password_path(:user)
            = signin_form.submit "Sign in", class: "btn btn-primary"

/ /

class SessionsController < Devise::SessionsController
    def create
        if request.xhr?
            resource = warden.authenticate!(:scope => resource_name, :recall => "#{controller_path}#failure")
            sign_in_and_redirect(resource_name, resource)
        else
            super
        end
    end

/ /

In my .js file...
...
$(document).ready(function() {
    $('#sign_in_user').on('ajax:error', function() {
        alert('error!');
    });
});

You need views/sessions/create.js.erb file, as a response to your ajax request. 您需要views / sessions / create.js.erb文件,作为对ajax请求的响应。

<% if current_user %>
  alert('Signed In');
<% else %>
  alert('error!');
<% end %>

And Change the following: 并更改以下内容:

sign_in_and_redirect(resource_name, resource)

to

sign_in(resource_name, resource)

(to avoid redirecting the user after sign_in) (以避免在登录后重定向用户)

Well, let me clarify the issue you have, so that you can pick what suits you. 好吧,让我澄清一下您遇到的问题,以便您可以选择适合自己的问题。 Every action should have a data response type either (html, js, json, ...). 每个动作都应具有数据响应类型(html,js,json等)。

First you need to change 首先你需要改变

sign_in_and_redirect(resource_name, resource)

to

sign_in(resource_name, resource)

Second thing you need to respond with json: 您需要用json响应的第二件事:

respond_to do |format|
    format.json { render :json =>  resource }
end

You may also need to change the Form to have data-type 您可能还需要更改表格以使其具有data-type

= form_for(resource, :as => resource_name, :url => session_path(resource_name) :remote => true, :html => {:'data-type' => 'json'})

For More details check this example , showing how to use AJAX callbacks Instead of .js.erb 有关更多详细信息,请检查此示例 ,该示例显示如何使用AJAX回调代替.js.erb

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM