我不在:
Rails 3.0.9
设计1.4.7
的jquery-UJS
我做了什么?... 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);
});
3)我重新定义了会话控制器,如下所示:
def create
resource = warden.authenticate!(:scope => resource_name, :recall => "#{controller_path}#failure")
sign_in(resource_name, resource)
return render :json => { :success => true, :content => current_user.email }
end
def failure
return render:json => {:success => false, :errors => ["Login failed."]}
end
4)我已经设置了route.rb文件:
devise_for :users, :controllers => {:sessions => "sessions"}
一切正常......但是当我传递错误的电子邮件或密码时,我从用户模型(可能来自Devise)收到未被捕获的消息“电子邮件或密码错误”。但我想抓住这条消息并把它放在json对象中,像这样 return render:json =&gt; {:success =&gt; false,:errors =&gt; ////电子邮件或密码错误////}
问题是::召回不起作用。它看起来像Warden.authenticate!没有看到我的失败方法
答案 0 :(得分:4)
解决方案非常简单)
我必须设置 config / initializers / devise.rb :
config.http_authenticatable_on_xhr = false
config.navigational_formats = [:html, :json]