简体   繁体   中英

Rails 5 Devise login via AJAX using Bootstrap Modal

Currently, I am trying to get a regular Devise sign_in form working using AJAX using bootstrap modals. The sign_in part works fine on a server side - user get signed in, but the problem is I can't get front end request handling in place as JS is not firing.

sessions_controller.rb #create

  def create
    resource = User.find_for_database_authentication(email: params[:user][:email])
    return invalid_login_attempt unless resource

    if resource.valid_password?(params[:user][:password])
      sign_in :user, resource
      return render :json => {:success => true}
    end

    invalid_login_attempt
  end

This part works fine - user gets signed in with 200 "success" response in the request.

Sign in form (default one for devise)

<%= form_for(resource, url: session_path(:user), remote: true) do |f|  %>
  <%= f.email_field :email, autofocus: true, placeholder: 'Email', class: "form-control center-block" %>

  <%= f.password_field :password, placeholder: 'Password', autocomplete: "off", class: 'form-control center-block' %>

  <%= f.submit 'log in', class: 'center-block btn btn-sp btn-lg'%>
<% end %>

Form is pre-rendered with hidden Bootstrap modal which of course get visible on click.

And the JS which is not firing

$(document).on('ajax:success', '#new_user', function(){
  console.log("in");
});

I tried ajaxSuccess , older .bind syntax, putting JS at app level and rendered right after the form, but can't seem to get that JS to fire and I am running out of ideas, so any help is appreciated. Running on Rails 5, Ruby 2.3.1, jquery-rails 4.1.1.

我相信您可以将本文作为起点http://blog.andrewray.me/how-to-set-up-devise-ajax-authentication-with-rails-4-0/ ,然后进行隐藏/显示响应时的模态窗口。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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