Ruby on rails Rails帐户参数缺少主干“%.save()中的参数`
我正在尝试使用主干网1.1.2和Rails 4.2.beta1创建一个用户帐户 我打电话是:Ruby on rails Rails帐户参数缺少主干“%.save()中的参数`,ruby-on-rails,backbone.js,typescript,backbone-model,ruby-on-rails-4.2,Ruby On Rails,Backbone.js,Typescript,Backbone Model,Ruby On Rails 4.2,我正在尝试使用主干网1.1.2和Rails 4.2.beta1创建一个用户帐户 我打电话是: public create(){ var email:string = $('#create-account-email').val(), password:string = $('#create-account-password').val(), passconf:string = $('#create-account-password-confirm').val(); this.mo
public create(){
var email:string = $('#create-account-email').val(), password:string = $('#create-account-password').val(), passconf:string = $('#create-account-password-confirm').val();
this.model.set({email: email, password: password, password_confirmation: passconf});
this.model.save(null, {success: this.success, error: this.error});
}
它使用以下请求参数正确调用my webservice中的railscreate
方法:
{"email":"test@test.com","password":"123456","password_confirmation":"123456"}
{"email":"test@test.com",
"password":"123456",
"password_confirmation":"123456"}
它正在通过过滤方法
def account_params
#breakpoint
params.require(:account).permit(:password, :password_confirmation, :email)
end
但是如果我在上面提到的位置放置一个断点并检查params
对象,我会得到:
{"email"=>"test@test.com",
"password"=>"123456",
"password_confirmation"=>"123456",
"controller"=>"accounts",
"action"=>"create",
"account"=>{"email"=>"test@test.com"},
"format"=>"json"}
对我来说,乍一看是正确的。但是帐户创建失败,因为密码为nil
如果检查帐户参数的结果,我只得到:
{"email" => "test@test.com")
为什么帐户参数对象中不包含密码。我在rails和主干网的默认配置中使用了所有默认的sacapfolded代码。我找到了一个用于ruby gem的GitHub repo,并使用了其中包含的同步覆盖 我删除了一些不需要的项目,但这将主干属性包装到一个对象中,并以所需的格式将其发送到rails应用程序 因此,参数:
{"email":"test@test.com","password":"123456","password_confirmation":"123456"}
{"email":"test@test.com",
"password":"123456",
"password_confirmation":"123456"}
成为:
{"account":
{"email":"test@test.com",
"password":"123456",
"password_confirmation":"123456"}
}
这不是没有一点返工主干应用程序代码
由于我使用的是typescript,我必须使用以下签名将\u sync()
方法添加到主干.d.ts
文件中
declare module Backbone {
function _sync(method: string, model: Model, options?: JQueryAjaxSettings):any;
...
}
我还需要向主干网添加以下内容
class Model extends ModelBase implements OptionalDefaults {
paramRoot: string;
...
}
然后我将paramRoot
属性添加到我的模型中:
export class Account extends Backbone.Model {
public urlRoot: string;
public validation:any;
public paramRoot:string;
constructor(attributes?: any, options?: any){
this.paramRoot = 'account';
this.urlRoot = 'http://mydomain.fake/accounts';
this.validation = {
email: {
required: true,
pattern: 'email'
},
password: {
required: true,
minLength: 6
}
};
super(attributes, options);
}
}
还有其他方法允许\u sync()
方法和paramRoot
属性传递编译器错误,而无需修改主干.d.ts
文件,而是扩展对象并将其添加到另一个.d.ts
文件中。但这对我来说没问题,因为我计划在未来的项目中使用它,我不介意稍微修改.d.ts
文件
希望这对将来的人有所帮助。你能发布你的表格吗?@Mandeep我为你编辑了上面的代码。这是一个表单,但我正在使用
$().val()
从表单中读取它,所以我为您包含了该代码。啊,好的,您如何将这些参数发送到服务器?看看你的情妇们,他们好像没有窝properly@Mandeep我现在只是让脊梁骨做这件事。我是否需要修改主干发送参数的方式。据我所知,主干+导轨可以正常工作不,它们不只是开箱即用。主干想要发送{p1:v1,p2:v2}
,但是Rails想要查看{model_name:{p1:v1,p2:v2}
。有一些宝石可以帮助你把东西粘在一起,或者你可以自己选择一个sync
override。在谷歌上搜索“主干轨”。