1. 程式人生 > >ajax 登錄

ajax 登錄

cati hold date sign -a oca validate authent sub

= form_for @user, :url => login_validate_path(@user), :html => {:class => "form-horizontal", :id => "sign-in-user"}, :format => :json, :remote => true do|f| 
  .samples-error-msg
  .form-group
    .col-md-12
      = f.email_field :email, :class => "form-control samples-email-input
", :placeholder => "郵箱" .form-group .col-md-12 = f.password_field :password, autocomplete: "off", :class => "form-control samples-pwd-input", :placeholder => "密碼" .col-md-12.sample-login-content = link_to "註冊", new_user_registration_path, :target => "_blank" .form
-group .col-md-12 %button.btn.btn-block.samples-modal-btn#samples-login-btn 登 錄
function loginModal(url) {
  $("form#sign-in-user").bind("ajax:success", function(e, data){
    if (data.success) {
      setTimeout(location.href = url, 6000);
    } else {
      $(".samples-error-msg").text("用戶名或密碼錯誤");
      $(
"#samples-login-btn").html("登錄"); $("#samples-login-btn").removeAttr("disabled"); return; } }); $("form#sign-in-user").submit(function(){ $(".samples-error-msg").html(""); $("#samples-login-btn").html("<span class=‘glyphicon glyphicon-refresh glyphicon-refresh-animate‘></span> 登錄中..."); $("#samples-login-btn").attr("disabled", "disabled"); }); }

class Users::SessionsController < Devise::SessionsController
  layout "application_home"

  def create
    super 
  end
 
  def after_sign_in_path_for(resource)
    root_path
  end

  def user_validate
    resource = User.find_for_database_authentication(email: params[:user][:email])
    return render :json => {:success => false} unless resource

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

ajax 登錄