0
レールにAJAXでの作業の重複チェックを取得できません。 レコードが存在するものの、エラーが現れることはありません。以下は、私のコードはは私が<strong><em>重複メール</em></strong>をチェック<strong><em>アヤックス</em></strong>を持つメソッドを呼び出すようにしようとしている
#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 "https://stackoverflow.com/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"=>"[email protected]"}
Redirected to http://localhost:3000/
Filter chain halted as :login_check rendered or redirected
Completed 302 Found in 0ms
問題です。
私は間違っていますか?
'フィルタチェーンが次のように停止しました:login_checkがレンダリングまたはリダイレクトされました。また、 'staus == 'true'は' staus.status == 'true' 'でなければなりません。 –
問題は 'success:function(staus){'、** status **は、あなたのコード 'if(staus == 'true'){'がfalseに評価された場合、 '{status:" true "}'あなたが必要とするのは 'if(staus.status == 'true'){'または 'function(staus)'の名前を 'function(data)'に変更し、 'data.status'として使用することです。 – Sajan
@ネスレのポインタ!私は間違いの誤りだった。あなたが正しいです。私はそのコントローラに 'skip_before_filter:login_check'を追加し、' status == 'true'を 'status.status == 'true'に変更する必要があります。今すぐうまく動作します。ありがとう! – Pavan