我想检查用户名唯一性 onBlur jQuery 事件。我添加了特殊属性来查找我需要检查的字段,如下所示:
<input data-validate="/checkname" id="user_email" name="user[email]" size="30" type="email" value="">
并在 RegistrationsController 中添加了操作(使用设计):
def checkname
if User.where('user = ?', params[:user]).count == 0
render :nothing => true, :status => 200
else
render :nothing => true, :status => 409
end
return
end
...routes...
checkname GET /checkname(.:format) devise/registrations#checkname
我想从 jQuery 中调用它,发送参数添加 css 类取决于 checkname 操作:
$('[data-validate]').blur(function() {
$this = $(this);
$.get($this.data('validate'), {
email: $this.val()
}).success(function() {
$this.addClass('valid');
}).error(function() {
$this.addClass('error');
});
});
编辑:请求似乎正在调用,但可能调用错误。从 Firebug 网络选项卡:
http://127.0.0.1:3000/checkname?email=new%40mail.com 404 not found
在电子邮件字段中输入 new@mail.com 时。
这段代码不断向我的领域添加错误类,所以我有一些错误 - 有人可以建议在哪里吗?