无法在rails中使用ajax进行复制检查

时间:2016-11-28 06:34:53

标签: jquery ruby-on-rails ajax

我尝试使用 ajax 调用方法来检查 重复的电子邮件 。以下是我的代码

#users_controller.rb
def check_email
  email = User.find_by(email: params[:email]) #record exists

  if email
    #return true
    render :json => { status: "true" }
  else
    return false
  end
end

#view code

<input type="email" name="email" id="s_email"  placeholder="Email"/>
<span id="duplicate_email"></span>

#jquery
$('#s_email').on('change keyup paste', function() {

  var email = $('#s_email').val();
  $.ajax({
    url: '/users/check_email',
    data: {"email": email},
    type: 'GET',
    dataType: 'json',

    success: function(status){
      if (status == 'true') {
        $('#duplicate_email').text("Email Already Exists! Try Different Email");
      }
    }
  });
});

#server log
Started GET "/users/check_email?email=pavan%40xyz.in" for 127.0.0.1 at 2016-11-28 11:45:02 +0530
Processing by UsersController#check_email as JSON
  Parameters: {"email"=>"pavan@xyz.in"}
Redirected to http://localhost:3000/
Filter chain halted as :login_check rendered or redirected
Completed 302 Found in 0ms

问题: 虽然记录存在,但错误从未出现过。

我做错了什么?

1 个答案:

答案 0 :(得分:1)

问题在于错误Filter chain halted as :login_check rendered or redirected看起来像是身份验证错误。根据您的操作代码,staus == 'true'也应为staus.status == 'true'