Pavan Pavan - 1 year ago 72
jQuery Question

Can't get the duplication check working with ajax in rails

I'm trying to call a method with ajax which checks for duplicate email. Below is my code

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

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

#view code

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

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

var email = $('#s_email').val();
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?" for at 2016-11-28 11:45:02 +0530
Processing by UsersController#check_email as JSON
Parameters: {"email"=>""}
Redirected to http://localhost:3000/
Filter chain halted as :login_check rendered or redirected
Completed 302 Found in 0ms

Though the record exists, the error never shows up.

What I'm doing wrong?

Answer Source

The problem is in the error Filter chain halted as :login_check rendered or redirected looks like an authentication error. Also staus == 'true' should be staus.status == 'true' according to your action code.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download