我正在尝试设置一个使用 Backbone 和 Devise 进行注册的 Rails 应用程序。
Chrome 控制台中的错误回调中的响应文本说
responseText: "{"errors":{"email":["can't be blank"],"password":["can't be blank"]}}"
但是,服务器中的日志显示无法处理的实体
Parameters: {"email"=>"pp@rr.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "registration"=>{"email"=>"pp@rr.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}}
(0.1ms) begin transaction
(0.1ms) rollback transaction
Completed 422 Unprocessable Entity in 4ms (Views: 0.3ms | ActiveRecord: 0.1ms)
我有一个 Backbone 用户模型,可以设置保存的 url
UserRegistration = Backbone.Model.extend({
url: '/users.json',
paramRoot: 'user',
defaults: {
"email": "",
"password": "",
"password_confirmation": ""
}
});
在关联视图中,我从注册表单中获取属性,在模型中设置它们,然后保存模型
var email = $('#email').val();
var password_confirmation = $('#password_confirmation').val();
var password = $('#password').val();
this.model.set({email : email, password_confirmation: password_confirmation, password: password})
this.model.save(this.model.attributes, {
success: function(userSession, response) {
console.log("success");
console.log(userSession);
console.log(response);
console.log(this.model.url);
},
error: function(userSession, response) {
console.log("error");
console.log(userSession);
console.log(response);
}
});
}
设置模型属性后(保存前)我做了一个console.log(this.model.attributes),它们被设置了
Object {email: "oo@gmail.com", password: "snowy", password_confirmation: "snowy"}
我的用户模型看起来像这样
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me
end
任何人都可以提出任何建议吗?
最近的 Devise 版本仅响应 html 存在一些问题,因此我安装了 Devise 2.1.2 以使其响应 json 以使其与 Backbone 兼容。这不是这里的问题。