Les*_*iev 5 devise ruby-on-rails-3
我在:
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);
});
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
4)我已经设置了route.rb文件:
devise_for :users, :controllers => {:sessions => "sessions"}
Run Code Online (Sandbox Code Playgroud)
一切正常......但是当我传递错误的电子邮件或密码时,我从用户模型(可能来自Devise)收到未被发送的消息"电子邮件或密码错误".但是我想抓住这条消息并把它放在json对象中这样的返回渲染:json => {:success => false,:errors => ////电子邮件或密码错误////}
问题是:召回不起作用.它看起来像Warden.authenticate!没有看到我的失败方法
解决方案非常简单)
我必须在config/initializers/devise.rb中设置:
config.http_authenticatable_on_xhr = false
config.navigational_formats = [:html, :json]
Run Code Online (Sandbox Code Playgroud)