Ruby on rails 3 Rails 3设计Ajax登录捕获错误消息
我在:Ruby on rails 3 Rails 3设计Ajax登录捕获错误消息,ruby-on-rails-3,devise,Ruby On Rails 3,Devise,我在: Rails 3.0.9 设计1.4.7 jqueryujs 我做了什么?… 1) 我已使用data remote=“true”设置登录表单。 2) 我已经设置了登录jquery处理程序: ('form#user-reg-form') .bind("ajax:beforeSend", function(evt, xhr, settings){ }) .bind("ajax:success", function(evt, data, status, xhr){
Rails 3.0.9
设计1.4.7
jqueryujs 我做了什么?…
1) 我已使用data remote=“true”设置登录表单。
2) 我已经设置了登录jquery处理程序:
('form#user-reg-form')
.bind("ajax:beforeSend", function(evt, xhr, settings){
})
.bind("ajax:success", function(evt, data, status, xhr){
$('#comments').append(xhr.responseText);
})
.bind('ajax:complete', function(evt, xhr, status){
})
.bind("ajax:error", function(evt, xhr, status, error){
try {
errors = $.parseJSON(xhr.responseText);
} catch(err) {
errors = {message: "Please reload the page and try again"};
}
errorText = "There were errors with the submission: \n<ul>";
for ( error in errors ) {
errorText += "<li>" + error + ': ' + errors[error] + "</li> ";
}
errorText += "</ul>";
// Insert error list into form
$form.find('div.validation-error').html(errorText);
});
4) 我已在route.rb文件中设置了我的路径:
devise_for :users, :controllers => {:sessions => "sessions"}
一切正常…但当我传递错误的电子邮件或密码时,我会收到来自用户模型(可能来自Desive)的未捕获消息“电子邮件或密码错误”。但我想捕获此消息并将其放入json对象中,类似这样的内容return render:json=>{:success=>false,:errors=>///电子邮件或密码错误////}
问题是::回忆不起作用。看起来像是那个监狱长!没有看到我的失败方法解决方案非常简单)
我必须在config/initializers/develope.rb中设置:
config.http_authenticatable_on_xhr = false
config.navigational_formats = [:html, :json]
config.http_authenticatable_on_xhr = false
config.navigational_formats = [:html, :json]